Skip to content

Commit

Permalink
supporting find-my-way router override
Browse files Browse the repository at this point in the history
  • Loading branch information
jkyberneees committed Jan 3, 2019
1 parent 40cc09d commit ac12400
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const service = require('restana')({
### Configuration
- `server`: Allows to override the HTTP server instance to be used.
- `routerFactory`: Router factory function to allow default `find-my-way` router override.
- `prioRequestsProcessing`: If `TRUE`, HTTP requests processing/handling is prioritized using `setImmediate`. Default value: `TRUE`
- `ignoreTrailingSlash`: If `TRUE`, trailing slashes on routes are ignored. Default value: `FALSE`
- `allowUnsafeRegex`: If `TRUE`, potentially catastrophic exponential-time regular expressions are disabled. Default value: `FALSE`
Expand All @@ -45,7 +46,19 @@ const service = require('restana')({
});
```

### Creating the micro-service interface
#### Providing a router factory method:
> In this example we use `anumargak` router instead of `find-my-way`.
```js
const anumargak = require('anumargak')
const service = require('restana')({
routerFactory: (options) => {
return anumargak(options)
}
})
...
```

### Creating a micro-service
```js
const bodyParser = require('body-parser');
service.use(bodyParser.json());
Expand Down Expand Up @@ -86,12 +99,12 @@ Supported HTTP methods:
const methods = ['get', 'delete', 'put', 'patch', 'post', 'head', 'options', 'trace'];
```

### Starting the service
#### Starting the service
```js
service.start(3000).then((server) => {});
```

### Stopping the service
#### Stopping the service
```js
service.close().then(()=> {});
```
Expand Down
13 changes: 13 additions & 0 deletions demos/router-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const anumargak = require('anumargak')

const service = require('./../index')({
routerFactory: (options) => {
console.log('creating anumargak router...')
return anumargak(options)
}
})

service.get('/hi', (req, res) => {
res.send('Hello World!')
})
service.start()
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ module.exports = (options = {}) => {
})
}

// creating request router instance
const router = requestRouter(options)
// creating request router instance, considers custom router override
const router = options.routerFactory ? options.routerFactory(options) : requestRouter(options)
// routes holder
const routes = {}
// global middlewares holder
Expand Down
31 changes: 31 additions & 0 deletions specs/router-factory.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* global describe, it */
const request = require('supertest')

describe('Router Factory - overriding find-my-way router with "anumargak"', () => {
let server
const anumargak = require('anumargak')

const service = require('../index')({
routerFactory: (options) => {
console.log('creating anumargak router...')
return anumargak(options)
}
})
service.get('/hello', (req, res) => {
res.send(200)
})

it('should start the service with "anumargak" router', async () => {
server = await service.start(~~process.env.PORT)
})

it('should GET 200 on /hello', async () => {
await request(server)
.get('/hello')
.expect(200)
})

it('should successfully terminate the service', async () => {
await service.close()
})
})

0 comments on commit ac12400

Please sign in to comment.