Skip to content

Commit e9d1da2

Browse files
zakuro9715typicode
authored andcommitted
Add --middlewares option (typicode#343)
* Add --middlewares option * Support multiple middlewares files
1 parent 6e1c434 commit e9d1da2

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,21 @@ Now you can access resources using additional routes.
254254
/blog/posts/1/show
255255
```
256256

257+
### Use middlewares
258+
259+
You can use express middlewares with `--middlewares` option
260+
261+
```js
262+
// middlewares.js
263+
module.exports = function (req, res, next) {
264+
res.Header('X-Hello', 'World')
265+
}
266+
```
267+
268+
```bash
269+
json-server db.json --middlewares middlewares.js
270+
```
271+
257272
### CLI usage
258273

259274
```
@@ -265,6 +280,7 @@ Options:
265280
--host, -H Set host [default: "0.0.0.0"]
266281
--watch, -w Watch file(s) [boolean]
267282
--routes, -r Path to routes file
283+
--middlewares, -m Path to middlewares file
268284
--static, -s Set static files directory
269285
--read-only, --ro Allow only GET requests [boolean]
270286
--no-cors, --nc Disable Cross-Origin Resource Sharing [boolean]

src/cli/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ module.exports = function () {
2828
alias: 'r',
2929
description: 'Path to routes file'
3030
},
31+
middlewares: {
32+
alias: 'm',
33+
description: 'Path to middlewares file'
34+
},
3135
static: {
3236
alias: 's',
3337
description: 'Set static files directory'

src/cli/run.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function prettyPrint (argv, object, rules) {
3434
console.log()
3535
}
3636

37-
function createApp (source, object, routes, argv) {
37+
function createApp (source, object, routes, middlewares, argv) {
3838
var app = jsonServer.create()
3939

4040
var router = jsonServer.router(
@@ -62,6 +62,10 @@ function createApp (source, object, routes, argv) {
6262
app.use(rewriter)
6363
}
6464

65+
if (middlewares) {
66+
app.use(middlewares)
67+
}
68+
6569
if (argv.delay) {
6670
app.use(pause(argv.delay))
6771
}
@@ -105,10 +109,19 @@ module.exports = function (argv) {
105109
var routes = JSON.parse(fs.readFileSync(argv.routes))
106110
}
107111

112+
// Load middlewares
113+
if (argv.middlewares) {
114+
if (!Array.isArray(argv.middlewares)) {
115+
argv.middlewares = [argv.middlewares]
116+
}
117+
console.log(chalk.gray(' Loading', argv.middlewares))
118+
var middlewares = argv.middlewares.map(function (m) { return require(path.resolve(m)) })
119+
}
120+
108121
console.log(chalk.gray(' Done'))
109122

110123
// Create app and server
111-
app = createApp(source, data, routes, argv)
124+
app = createApp(source, data, routes, middlewares, argv)
112125
server = app.listen(argv.port, argv.host)
113126

114127
// Enhance with a destroy function

test/cli/index.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('cli', function () {
2828
var request
2929
var dbFile
3030
var routesFile
31+
var middlewareFiles
3132

3233
beforeEach(function () {
3334
dbFile = tempWrite.sync(JSON.stringify({
@@ -41,6 +42,18 @@ describe('cli', function () {
4142
'/blog/': '/'
4243
}), 'routes.json')
4344

45+
middlewareFiles = [
46+
tempWrite.sync(
47+
'module.exports = function (req, res, next) {\n' +
48+
' res.header("X-Hello", "World")\n' +
49+
' next() }'
50+
, 'helloWorldMiddleware.js'),
51+
tempWrite.sync(
52+
'module.exports = function (req, res, next) {\n' +
53+
' res.header("X-Konnichiwa", "Sekai")\n' +
54+
' next() }'
55+
, 'helloWorldJaMiddlewares.js')
56+
]
4457
++PORT
4558
request = supertest('http://localhost:' + PORT)
4659
})
@@ -109,21 +122,38 @@ describe('cli', function () {
109122
})
110123
})
111124

112-
describe('db.json -r routes.json -i _id --read-only', function () {
125+
describe('db.json -r routes.json -m helloWorldMiddleware.js -i _id --read-only', function () {
113126
beforeEach(function (done) {
114-
child = cli([dbFile, '-r', routesFile, '-i', '_id', '--read-only'])
127+
child = cli([dbFile, '-r', routesFile, '-m', middlewareFiles[0], '-i', '_id', '--read-only'])
115128
serverReady(PORT, done)
116129
})
117130

118131
it('should use routes.json and _id as the identifier', function (done) {
119132
request.get('/blog/posts/2').expect(200, done)
120133
})
121134

135+
it('should apply middlewares', function (done) {
136+
request.get('/blog/posts/2').expect('X-Hello', 'World', done)
137+
})
138+
122139
it('should allow only GET requests', function (done) {
123140
request.post('/blog/posts').expect(403, done)
124141
})
125142
})
126143

144+
describe('db.json -m helloWorldMiddleware.js -m helloWorldJaMiddleware.js', function () {
145+
beforeEach(function (done) {
146+
child = cli([dbFile, '-m', middlewareFiles[0], '-m', middlewareFiles[1]])
147+
serverReady(PORT, done)
148+
})
149+
150+
it('should apply all middlewares', function (done) {
151+
request.get('/blog/posts')
152+
.expect('X-Hello', 'World')
153+
.expect('X-Konnichiwa', 'Sekai', done)
154+
})
155+
})
156+
127157
describe('db.json -d 1000', function () {
128158
beforeEach(function (done) {
129159
child = cli([dbFile, '-d', 1000])

0 commit comments

Comments
 (0)