Skip to content

Commit 6c6333f

Browse files
committed
Add static option
1 parent 6610adb commit 6c6333f

File tree

9 files changed

+96
-36
lines changed

9 files changed

+96
-36
lines changed

CHANGELOG.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@
44

55
### Changed
66

7-
* Automatically ignore unknown query parameters
7+
* `jsonServer.defaults` is now a function and can take an object.
8+
If you're using the project as a module, you need to update your code:
9+
10+
```js
11+
// Before
12+
jsonServer.defaults
13+
// After
14+
jsonServer.defaults()
15+
jsonServer.defaults({ static: '/some/path'})
16+
```
17+
18+
* Automatically ignore unknown query parameters.
819

920
```bash
1021
# Before
@@ -13,6 +24,14 @@ GET /posts?author=typicode&foo=bar # []
1324
GET /posts?author=typicode&foo=bar # [{...}, {...}]
1425
```
1526

27+
### Added
28+
29+
* CLI option for setting a custom static files directory.
30+
31+
```bash
32+
json-server --static some/path
33+
```
34+
1635
## [0.7.28][2015-09-09]
1736

1837
```bash

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,19 @@ GET /
154154

155155
### Static file server
156156

157-
You can use JSON Server to serve your HTML, JS and CSS, simply create a `./public` directory.
157+
You can use JSON Server to serve your HTML, JS and CSS, simply create a `./public` directory
158+
or use `--static`.
158159

159160
```bash
160161
mkdir public
161162
echo 'hello word' > public/index.html
162163
json-server db.json
163164
```
164165

166+
```bash
167+
json-server db.json --static ./static
168+
```
169+
165170
### Access from anywhere
166171

167172
You can access your fake API from anywhere using CORS and JSONP.
@@ -233,7 +238,7 @@ var jsonServer = require('json-server')
233238
var server = jsonServer.create()
234239

235240
// Set default middlewares (logger, static, cors and no-cache)
236-
server.use(jsonServer.defaults)
241+
server.use(jsonServer.defaults())
237242

238243
// Returns an Express router
239244
var router = jsonServer.router('db.json')

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: 'Load routes file'
3030
},
31+
static: {
32+
alias: 's',
33+
description: 'Set static files directory'
34+
},
3135
delay: {
3236
alias: 'd',
3337
description: 'Add delay to responses (ms)'

src/cli/run.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var fs = require('fs')
2+
var path = require('path')
23
var chalk = require('chalk')
34
var is = require('./utils/is')
45
var load = require('./utils/load')
@@ -40,7 +41,16 @@ function createApp (source, object, routes, argv) {
4041
object
4142
)
4243

43-
app.use(jsonServer.defaults)
44+
var defaults
45+
if (argv.static) {
46+
defaults = jsonServer.defaults({
47+
static: path.join(process.cwd(), argv.static)
48+
})
49+
} else {
50+
defaults = jsonServer.defaults()
51+
}
52+
53+
app.use(defaults)
4454

4555
if (routes) {
4656
var rewriter = jsonServer.rewriter(routes)

src/server/defaults.js

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,48 @@
11
var fs = require('fs')
2+
var path = require('path')
23
var express = require('express')
34
var logger = require('morgan')
45
var cors = require('cors')
56
var errorhandler = require('errorhandler')
67

7-
var arr = []
8+
module.exports = function (opts) {
9+
var userDir = path.join(process.cwd(), 'public')
10+
var defaultDir = path.join(__dirname, 'public')
11+
var staticDir = fs.existsSync(userDir) ?
12+
userDir :
13+
defaultDir
814

9-
// Logger
10-
arr.push(logger('dev', {
11-
skip: function (req, res) {
12-
return process.env.NODE_ENV === 'test' ||
13-
req.path === '/favicon.ico'
14-
}
15-
}))
15+
opts = opts || { static: staticDir }
1616

17-
// Enable CORS for all the requests, including static files
18-
arr.push(cors({ origin: true, credentials: true }))
17+
var arr = []
1918

20-
if (process.env.NODE_ENV === 'development') {
21-
// only use in development
22-
arr.push(errorhandler())
23-
}
19+
// Logger
20+
arr.push(logger('dev', {
21+
skip: function (req, res) {
22+
return process.env.NODE_ENV === 'test' ||
23+
req.path === '/favicon.ico'
24+
}
25+
}))
2426

25-
// Serve static files
26-
if (fs.existsSync(process.cwd() + '/public')) {
27-
arr.push(express.static(process.cwd() + '/public'))
28-
} else {
29-
arr.push(express.static(__dirname + '/public'))
30-
}
27+
// Enable CORS for all the requests, including static files
28+
arr.push(cors({ origin: true, credentials: true }))
3129

32-
// No cache for IE
33-
// https://support.microsoft.com/en-us/kb/234067
34-
arr.push(function (req, res, next) {
35-
res.header('Cache-Control', 'no-cache')
36-
res.header('Pragma', 'no-cache')
37-
res.header('Expires', '-1')
38-
next()
39-
})
30+
if (process.env.NODE_ENV === 'development') {
31+
// only use in development
32+
arr.push(errorhandler())
33+
}
4034

41-
module.exports = arr
35+
// Serve static files
36+
arr.push(express.static(opts.static))
37+
38+
// No cache for IE
39+
// https://support.microsoft.com/en-us/kb/234067
40+
arr.push(function (req, res, next) {
41+
res.header('Cache-Control', 'no-cache')
42+
res.header('Pragma', 'no-cache')
43+
res.header('Expires', '-1')
44+
next()
45+
})
46+
47+
return arr
48+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello

test/cli/index.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ describe('cli', function () {
7070
describe('http://jsonplaceholder.typicode.com/db', function () {
7171

7272
beforeEach(function (done) {
73-
this.timeout(10000)
7473
child = cli(['http://jsonplaceholder.typicode.com/db'])
75-
setTimeout(done, 9000)
74+
this.timeout(10000)
75+
serverReady(PORT, done)
7676
})
7777

7878
it('should support URL file', function (done) {
@@ -111,6 +111,19 @@ describe('cli', function () {
111111

112112
})
113113

114+
describe('db.json -s fixtures/public', function () {
115+
116+
beforeEach(function (done) {
117+
child = cli([dbFile, '-s', 'fixtures/public'])
118+
serverReady(PORT, done)
119+
})
120+
121+
it('should serve fixtures/public', function (done) {
122+
request.get('/').expect(/Hello/, done)
123+
})
124+
125+
})
126+
114127
// FIXME test fails on OS X and maybe on Windows
115128
// But manually updating db.json works...
116129
if (os.platform() === 'linux') {

test/server/plural.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('Server', function () {
4949

5050
server = jsonServer.create()
5151
router = jsonServer.router(db)
52-
server.use(jsonServer.defaults)
52+
server.use(jsonServer.defaults())
5353
server.use(jsonServer.rewriter({
5454
'/api/': '/',
5555
'/blog/posts/:id/show': '/posts/:id'

test/server/singular.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('Server', function () {
1919

2020
server = jsonServer.create()
2121
router = jsonServer.router(db)
22+
server.use(jsonServer.defaults())
2223
server.use(router)
2324
})
2425

0 commit comments

Comments
 (0)