Add --read-only option

This commit is contained in:
Typicode
2016-01-07 04:12:04 +01:00
parent d1205844c0
commit 672393e915
5 changed files with 37 additions and 14 deletions

View File

@ -1,5 +1,11 @@
# Change Log # Change Log
## [0.8.6][2015-01-07]
### Added
* CLI option `-ro/--read-only` to allow only GET requests
## [0.8.5][2015-12-28] ## [0.8.5][2015-12-28]
### Fixed ### Fixed
@ -16,7 +22,7 @@
### Added ### Added
* CLI option `-q/--quied` * CLI option `-q/--quiet`
* Nested route `POST /posts/1/comments` * Nested route `POST /posts/1/comments`
* Not equal operator `GET /posts?id_ne=1` * Not equal operator `GET /posts?id_ne=1`

View File

@ -32,6 +32,10 @@ module.exports = function () {
alias: 's', alias: 's',
description: 'Set static files directory' description: 'Set static files directory'
}, },
'read-only': {
alias: 'ro',
description: 'Allow only GET requests'
},
snapshots: { snapshots: {
alias: 'S', alias: 'S',
description: 'Set snapshots directory', description: 'Set snapshots directory',
@ -52,6 +56,7 @@ module.exports = function () {
} }
}) })
.boolean('watch') .boolean('watch')
.boolean('read-only')
.boolean('quiet') .boolean('quiet')
.help('help').alias('help', 'h') .help('help').alias('help', 'h')
.version(pkg.version).alias('version', 'v') .version(pkg.version).alias('version', 'v')
@ -63,5 +68,4 @@ module.exports = function () {
.argv .argv
run(argv) run(argv)
} }

View File

@ -42,18 +42,16 @@ function createApp (source, object, routes, argv) {
object object
) )
var defaults var defaultsOpts = {
if (argv.static) { logger: !argv.quiet,
defaults = jsonServer.defaults({ readOnly: argv.readOnly
logger: !argv.quiet,
static: path.join(process.cwd(), argv.static)
})
} else {
defaults = jsonServer.defaults({
logger: !argv.quiet
})
} }
if (argv.static) {
defaultsOpts.static = path.join(process.cwd(), argv.static)
}
var defaults = jsonServer.defaults(defaultsOpts)
app.use(defaults) app.use(defaults)
if (routes) { if (routes) {

View File

@ -47,5 +47,16 @@ module.exports = function (opts) {
next() next()
}) })
// Read-only
if (opts.readOnly) {
arr.push(function (req, res, next) {
if (req.method === 'GET') {
next() // Continue
} else {
res.sendStatus(403) // Forbidden
}
})
}
return arr return arr
} }

View File

@ -82,10 +82,10 @@ describe('cli', function () {
}) })
describe('db.json -r routes.json -i _id', function () { describe('db.json -r routes.json -i _id --read-only', function () {
beforeEach(function (done) { beforeEach(function (done) {
child = cli([dbFile, '-r', routesFile, '-i', '_id']) child = cli([dbFile, '-r', routesFile, '-i', '_id', '--read-only'])
serverReady(PORT, done) serverReady(PORT, done)
}) })
@ -93,6 +93,10 @@ describe('cli', function () {
request.get('/blog/posts/2').expect(200, done) request.get('/blog/posts/2').expect(200, done)
}) })
it('should allow only GET requests', function (done) {
request.post('/blog/posts').expect(403, done)
})
}) })
describe('db.json -d 1000', function () { describe('db.json -d 1000', function () {