Skip to content

Commit

Permalink
Merge pull request #13 from jkyberneees/allow-router-override
Browse files Browse the repository at this point in the history
Allow router override (v2.7.0)
  • Loading branch information
jkyberneees authored Jan 3, 2019
2 parents 40cc09d + 6d60554 commit 377999a
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 7 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
37 changes: 36 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "restana",
"version": "2.6.0",
"version": "2.7.0",
"description": "Super fast and minimalist web framework for building REST micro-services.",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -37,6 +37,7 @@
"find-my-way": "^1.17.0"
},
"devDependencies": {
"anumargak": "^2.0.2",
"chai": "^4.2.0",
"express": "^4.16.4",
"fastify": "^1.13.3",
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 377999a

Please sign in to comment.