Skip to content

Commit 672393e

Browse files
committed
Add --read-only option
1 parent d120584 commit 672393e

File tree

5 files changed

+36
-13
lines changed

5 files changed

+36
-13
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## [0.8.6][2015-01-07]
4+
5+
### Added
6+
7+
* CLI option `-ro/--read-only` to allow only GET requests
8+
39
## [0.8.5][2015-12-28]
410

511
### Fixed
@@ -16,7 +22,7 @@
1622

1723
### Added
1824

19-
* CLI option `-q/--quied`
25+
* CLI option `-q/--quiet`
2026
* Nested route `POST /posts/1/comments`
2127
* Not equal operator `GET /posts?id_ne=1`
2228

src/cli/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ module.exports = function () {
3232
alias: 's',
3333
description: 'Set static files directory'
3434
},
35+
'read-only': {
36+
alias: 'ro',
37+
description: 'Allow only GET requests'
38+
},
3539
snapshots: {
3640
alias: 'S',
3741
description: 'Set snapshots directory',
@@ -52,6 +56,7 @@ module.exports = function () {
5256
}
5357
})
5458
.boolean('watch')
59+
.boolean('read-only')
5560
.boolean('quiet')
5661
.help('help').alias('help', 'h')
5762
.version(pkg.version).alias('version', 'v')
@@ -63,5 +68,4 @@ module.exports = function () {
6368
.argv
6469

6570
run(argv)
66-
6771
}

src/cli/run.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,16 @@ function createApp (source, object, routes, argv) {
4242
object
4343
)
4444

45-
var defaults
45+
var defaultsOpts = {
46+
logger: !argv.quiet,
47+
readOnly: argv.readOnly
48+
}
49+
4650
if (argv.static) {
47-
defaults = jsonServer.defaults({
48-
logger: !argv.quiet,
49-
static: path.join(process.cwd(), argv.static)
50-
})
51-
} else {
52-
defaults = jsonServer.defaults({
53-
logger: !argv.quiet
54-
})
51+
defaultsOpts.static = path.join(process.cwd(), argv.static)
5552
}
5653

54+
var defaults = jsonServer.defaults(defaultsOpts)
5755
app.use(defaults)
5856

5957
if (routes) {

src/server/defaults.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,16 @@ module.exports = function (opts) {
4747
next()
4848
})
4949

50+
// Read-only
51+
if (opts.readOnly) {
52+
arr.push(function (req, res, next) {
53+
if (req.method === 'GET') {
54+
next() // Continue
55+
} else {
56+
res.sendStatus(403) // Forbidden
57+
}
58+
})
59+
}
60+
5061
return arr
5162
}

test/cli/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,21 @@ describe('cli', function () {
8282

8383
})
8484

85-
describe('db.json -r routes.json -i _id', function () {
85+
describe('db.json -r routes.json -i _id --read-only', function () {
8686

8787
beforeEach(function (done) {
88-
child = cli([dbFile, '-r', routesFile, '-i', '_id'])
88+
child = cli([dbFile, '-r', routesFile, '-i', '_id', '--read-only'])
8989
serverReady(PORT, done)
9090
})
9191

9292
it('should use routes.json and _id as the identifier', function (done) {
9393
request.get('/blog/posts/2').expect(200, done)
9494
})
9595

96+
it('should allow only GET requests', function (done) {
97+
request.post('/blog/posts').expect(403, done)
98+
})
99+
96100
})
97101

98102
describe('db.json -d 1000', function () {

0 commit comments

Comments
 (0)