diff --git a/README.md b/README.md index 1b207a1..8601ff6 100644 --- a/README.md +++ b/README.md @@ -254,6 +254,21 @@ Now you can access resources using additional routes. /blog/posts/1/show ``` +### Use middlewares + +You can use express middlewares with `--middlewares` option + +```js +// middlewares.js +module.exports = function (req, res, next) { + res.Header('X-Hello', 'World') +} +``` + +```bash +json-server db.json --middlewares middlewares.js +``` + ### CLI usage ``` @@ -265,6 +280,7 @@ Options: --host, -H Set host [default: "0.0.0.0"] --watch, -w Watch file(s) [boolean] --routes, -r Path to routes file + --middlewares, -m Path to middlewares file --static, -s Set static files directory --read-only, --ro Allow only GET requests [boolean] --no-cors, --nc Disable Cross-Origin Resource Sharing [boolean] diff --git a/src/cli/index.js b/src/cli/index.js index fa7692c..88abbc0 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -28,6 +28,10 @@ module.exports = function () { alias: 'r', description: 'Path to routes file' }, + middlewares: { + alias: 'm', + description: 'Path to middlewares file' + }, static: { alias: 's', description: 'Set static files directory' diff --git a/src/cli/run.js b/src/cli/run.js index 1369226..897f4f6 100644 --- a/src/cli/run.js +++ b/src/cli/run.js @@ -34,7 +34,7 @@ function prettyPrint (argv, object, rules) { console.log() } -function createApp (source, object, routes, argv) { +function createApp (source, object, routes, middlewares, argv) { var app = jsonServer.create() var router = jsonServer.router( @@ -62,6 +62,10 @@ function createApp (source, object, routes, argv) { app.use(rewriter) } + if (middlewares) { + app.use(middlewares) + } + if (argv.delay) { app.use(pause(argv.delay)) } @@ -105,10 +109,19 @@ module.exports = function (argv) { var routes = JSON.parse(fs.readFileSync(argv.routes)) } + // Load middlewares + if (argv.middlewares) { + if (!Array.isArray(argv.middlewares)) { + argv.middlewares = [argv.middlewares] + } + console.log(chalk.gray(' Loading', argv.middlewares)) + var middlewares = argv.middlewares.map(function (m) { return require(path.resolve(m)) }) + } + console.log(chalk.gray(' Done')) // Create app and server - app = createApp(source, data, routes, argv) + app = createApp(source, data, routes, middlewares, argv) server = app.listen(argv.port, argv.host) // Enhance with a destroy function diff --git a/test/cli/index.js b/test/cli/index.js index dec7787..4a0ca8d 100644 --- a/test/cli/index.js +++ b/test/cli/index.js @@ -28,6 +28,7 @@ describe('cli', function () { var request var dbFile var routesFile + var middlewareFiles beforeEach(function () { dbFile = tempWrite.sync(JSON.stringify({ @@ -41,6 +42,18 @@ describe('cli', function () { '/blog/': '/' }), 'routes.json') + middlewareFiles = [ + tempWrite.sync( + 'module.exports = function (req, res, next) {\n' + + ' res.header("X-Hello", "World")\n' + + ' next() }' + , 'helloWorldMiddleware.js'), + tempWrite.sync( + 'module.exports = function (req, res, next) {\n' + + ' res.header("X-Konnichiwa", "Sekai")\n' + + ' next() }' + , 'helloWorldJaMiddlewares.js') + ] ++PORT request = supertest('http://localhost:' + PORT) }) @@ -109,9 +122,9 @@ describe('cli', function () { }) }) - describe('db.json -r routes.json -i _id --read-only', function () { + describe('db.json -r routes.json -m helloWorldMiddleware.js -i _id --read-only', function () { beforeEach(function (done) { - child = cli([dbFile, '-r', routesFile, '-i', '_id', '--read-only']) + child = cli([dbFile, '-r', routesFile, '-m', middlewareFiles[0], '-i', '_id', '--read-only']) serverReady(PORT, done) }) @@ -119,11 +132,28 @@ describe('cli', function () { request.get('/blog/posts/2').expect(200, done) }) + it('should apply middlewares', function (done) { + request.get('/blog/posts/2').expect('X-Hello', 'World', done) + }) + it('should allow only GET requests', function (done) { request.post('/blog/posts').expect(403, done) }) }) + describe('db.json -m helloWorldMiddleware.js -m helloWorldJaMiddleware.js', function () { + beforeEach(function (done) { + child = cli([dbFile, '-m', middlewareFiles[0], '-m', middlewareFiles[1]]) + serverReady(PORT, done) + }) + + it('should apply all middlewares', function (done) { + request.get('/blog/posts') + .expect('X-Hello', 'World') + .expect('X-Konnichiwa', 'Sekai', done) + }) + }) + describe('db.json -d 1000', function () { beforeEach(function (done) { child = cli([dbFile, '-d', 1000])