mirror of
https://github.com/typicode/json-server.git
synced 2025-07-28 20:52:08 +08:00
Add static option
This commit is contained in:
21
CHANGELOG.md
21
CHANGELOG.md
@ -4,7 +4,18 @@
|
|||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
* Automatically ignore unknown query parameters
|
* `jsonServer.defaults` is now a function and can take an object.
|
||||||
|
If you're using the project as a module, you need to update your code:
|
||||||
|
|
||||||
|
```js
|
||||||
|
// Before
|
||||||
|
jsonServer.defaults
|
||||||
|
// After
|
||||||
|
jsonServer.defaults()
|
||||||
|
jsonServer.defaults({ static: '/some/path'})
|
||||||
|
```
|
||||||
|
|
||||||
|
* Automatically ignore unknown query parameters.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Before
|
# Before
|
||||||
@ -13,6 +24,14 @@ GET /posts?author=typicode&foo=bar # []
|
|||||||
GET /posts?author=typicode&foo=bar # [{...}, {...}]
|
GET /posts?author=typicode&foo=bar # [{...}, {...}]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
* CLI option for setting a custom static files directory.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
json-server --static some/path
|
||||||
|
```
|
||||||
|
|
||||||
## [0.7.28][2015-09-09]
|
## [0.7.28][2015-09-09]
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
@ -154,7 +154,8 @@ GET /
|
|||||||
|
|
||||||
### Static file server
|
### Static file server
|
||||||
|
|
||||||
You can use JSON Server to serve your HTML, JS and CSS, simply create a `./public` directory.
|
You can use JSON Server to serve your HTML, JS and CSS, simply create a `./public` directory
|
||||||
|
or use `--static`.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
mkdir public
|
mkdir public
|
||||||
@ -162,6 +163,10 @@ echo 'hello word' > public/index.html
|
|||||||
json-server db.json
|
json-server db.json
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
json-server db.json --static ./static
|
||||||
|
```
|
||||||
|
|
||||||
### Access from anywhere
|
### Access from anywhere
|
||||||
|
|
||||||
You can access your fake API from anywhere using CORS and JSONP.
|
You can access your fake API from anywhere using CORS and JSONP.
|
||||||
@ -233,7 +238,7 @@ var jsonServer = require('json-server')
|
|||||||
var server = jsonServer.create()
|
var server = jsonServer.create()
|
||||||
|
|
||||||
// Set default middlewares (logger, static, cors and no-cache)
|
// Set default middlewares (logger, static, cors and no-cache)
|
||||||
server.use(jsonServer.defaults)
|
server.use(jsonServer.defaults())
|
||||||
|
|
||||||
// Returns an Express router
|
// Returns an Express router
|
||||||
var router = jsonServer.router('db.json')
|
var router = jsonServer.router('db.json')
|
||||||
|
@ -28,6 +28,10 @@ module.exports = function () {
|
|||||||
alias: 'r',
|
alias: 'r',
|
||||||
description: 'Load routes file'
|
description: 'Load routes file'
|
||||||
},
|
},
|
||||||
|
static: {
|
||||||
|
alias: 's',
|
||||||
|
description: 'Set static files directory'
|
||||||
|
},
|
||||||
delay: {
|
delay: {
|
||||||
alias: 'd',
|
alias: 'd',
|
||||||
description: 'Add delay to responses (ms)'
|
description: 'Add delay to responses (ms)'
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
var fs = require('fs')
|
var fs = require('fs')
|
||||||
|
var path = require('path')
|
||||||
var chalk = require('chalk')
|
var chalk = require('chalk')
|
||||||
var is = require('./utils/is')
|
var is = require('./utils/is')
|
||||||
var load = require('./utils/load')
|
var load = require('./utils/load')
|
||||||
@ -40,7 +41,16 @@ function createApp (source, object, routes, argv) {
|
|||||||
object
|
object
|
||||||
)
|
)
|
||||||
|
|
||||||
app.use(jsonServer.defaults)
|
var defaults
|
||||||
|
if (argv.static) {
|
||||||
|
defaults = jsonServer.defaults({
|
||||||
|
static: path.join(process.cwd(), argv.static)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
defaults = jsonServer.defaults()
|
||||||
|
}
|
||||||
|
|
||||||
|
app.use(defaults)
|
||||||
|
|
||||||
if (routes) {
|
if (routes) {
|
||||||
var rewriter = jsonServer.rewriter(routes)
|
var rewriter = jsonServer.rewriter(routes)
|
||||||
|
@ -1,41 +1,48 @@
|
|||||||
var fs = require('fs')
|
var fs = require('fs')
|
||||||
|
var path = require('path')
|
||||||
var express = require('express')
|
var express = require('express')
|
||||||
var logger = require('morgan')
|
var logger = require('morgan')
|
||||||
var cors = require('cors')
|
var cors = require('cors')
|
||||||
var errorhandler = require('errorhandler')
|
var errorhandler = require('errorhandler')
|
||||||
|
|
||||||
var arr = []
|
module.exports = function (opts) {
|
||||||
|
var userDir = path.join(process.cwd(), 'public')
|
||||||
|
var defaultDir = path.join(__dirname, 'public')
|
||||||
|
var staticDir = fs.existsSync(userDir) ?
|
||||||
|
userDir :
|
||||||
|
defaultDir
|
||||||
|
|
||||||
// Logger
|
opts = opts || { static: staticDir }
|
||||||
arr.push(logger('dev', {
|
|
||||||
skip: function (req, res) {
|
var arr = []
|
||||||
return process.env.NODE_ENV === 'test' ||
|
|
||||||
req.path === '/favicon.ico'
|
// Logger
|
||||||
|
arr.push(logger('dev', {
|
||||||
|
skip: function (req, res) {
|
||||||
|
return process.env.NODE_ENV === 'test' ||
|
||||||
|
req.path === '/favicon.ico'
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
|
// Enable CORS for all the requests, including static files
|
||||||
|
arr.push(cors({ origin: true, credentials: true }))
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
// only use in development
|
||||||
|
arr.push(errorhandler())
|
||||||
}
|
}
|
||||||
}))
|
|
||||||
|
|
||||||
// Enable CORS for all the requests, including static files
|
// Serve static files
|
||||||
arr.push(cors({ origin: true, credentials: true }))
|
arr.push(express.static(opts.static))
|
||||||
|
|
||||||
if (process.env.NODE_ENV === 'development') {
|
// No cache for IE
|
||||||
// only use in development
|
// https://support.microsoft.com/en-us/kb/234067
|
||||||
arr.push(errorhandler())
|
arr.push(function (req, res, next) {
|
||||||
|
res.header('Cache-Control', 'no-cache')
|
||||||
|
res.header('Pragma', 'no-cache')
|
||||||
|
res.header('Expires', '-1')
|
||||||
|
next()
|
||||||
|
})
|
||||||
|
|
||||||
|
return arr
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serve static files
|
|
||||||
if (fs.existsSync(process.cwd() + '/public')) {
|
|
||||||
arr.push(express.static(process.cwd() + '/public'))
|
|
||||||
} else {
|
|
||||||
arr.push(express.static(__dirname + '/public'))
|
|
||||||
}
|
|
||||||
|
|
||||||
// No cache for IE
|
|
||||||
// https://support.microsoft.com/en-us/kb/234067
|
|
||||||
arr.push(function (req, res, next) {
|
|
||||||
res.header('Cache-Control', 'no-cache')
|
|
||||||
res.header('Pragma', 'no-cache')
|
|
||||||
res.header('Expires', '-1')
|
|
||||||
next()
|
|
||||||
})
|
|
||||||
|
|
||||||
module.exports = arr
|
|
||||||
|
1
test/cli/fixtures/public/index.html
Normal file
1
test/cli/fixtures/public/index.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
Hello
|
@ -70,9 +70,9 @@ describe('cli', function () {
|
|||||||
describe('http://jsonplaceholder.typicode.com/db', function () {
|
describe('http://jsonplaceholder.typicode.com/db', function () {
|
||||||
|
|
||||||
beforeEach(function (done) {
|
beforeEach(function (done) {
|
||||||
this.timeout(10000)
|
|
||||||
child = cli(['http://jsonplaceholder.typicode.com/db'])
|
child = cli(['http://jsonplaceholder.typicode.com/db'])
|
||||||
setTimeout(done, 9000)
|
this.timeout(10000)
|
||||||
|
serverReady(PORT, done)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should support URL file', function (done) {
|
it('should support URL file', function (done) {
|
||||||
@ -111,6 +111,19 @@ describe('cli', function () {
|
|||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('db.json -s fixtures/public', function () {
|
||||||
|
|
||||||
|
beforeEach(function (done) {
|
||||||
|
child = cli([dbFile, '-s', 'fixtures/public'])
|
||||||
|
serverReady(PORT, done)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should serve fixtures/public', function (done) {
|
||||||
|
request.get('/').expect(/Hello/, done)
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
// FIXME test fails on OS X and maybe on Windows
|
// FIXME test fails on OS X and maybe on Windows
|
||||||
// But manually updating db.json works...
|
// But manually updating db.json works...
|
||||||
if (os.platform() === 'linux') {
|
if (os.platform() === 'linux') {
|
||||||
|
@ -49,7 +49,7 @@ describe('Server', function () {
|
|||||||
|
|
||||||
server = jsonServer.create()
|
server = jsonServer.create()
|
||||||
router = jsonServer.router(db)
|
router = jsonServer.router(db)
|
||||||
server.use(jsonServer.defaults)
|
server.use(jsonServer.defaults())
|
||||||
server.use(jsonServer.rewriter({
|
server.use(jsonServer.rewriter({
|
||||||
'/api/': '/',
|
'/api/': '/',
|
||||||
'/blog/posts/:id/show': '/posts/:id'
|
'/blog/posts/:id/show': '/posts/:id'
|
||||||
|
@ -19,6 +19,7 @@ describe('Server', function () {
|
|||||||
|
|
||||||
server = jsonServer.create()
|
server = jsonServer.create()
|
||||||
router = jsonServer.router(db)
|
router = jsonServer.router(db)
|
||||||
|
server.use(jsonServer.defaults())
|
||||||
server.use(router)
|
server.use(router)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user