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
|
||||
|
||||
* 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
|
||||
# Before
|
||||
@ -13,6 +24,14 @@ 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]
|
||||
|
||||
```bash
|
||||
|
@ -154,7 +154,8 @@ GET /
|
||||
|
||||
### 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
|
||||
mkdir public
|
||||
@ -162,6 +163,10 @@ echo 'hello word' > public/index.html
|
||||
json-server db.json
|
||||
```
|
||||
|
||||
```bash
|
||||
json-server db.json --static ./static
|
||||
```
|
||||
|
||||
### Access from anywhere
|
||||
|
||||
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()
|
||||
|
||||
// Set default middlewares (logger, static, cors and no-cache)
|
||||
server.use(jsonServer.defaults)
|
||||
server.use(jsonServer.defaults())
|
||||
|
||||
// Returns an Express router
|
||||
var router = jsonServer.router('db.json')
|
||||
|
@ -28,6 +28,10 @@ module.exports = function () {
|
||||
alias: 'r',
|
||||
description: 'Load routes file'
|
||||
},
|
||||
static: {
|
||||
alias: 's',
|
||||
description: 'Set static files directory'
|
||||
},
|
||||
delay: {
|
||||
alias: 'd',
|
||||
description: 'Add delay to responses (ms)'
|
||||
|
@ -1,4 +1,5 @@
|
||||
var fs = require('fs')
|
||||
var path = require('path')
|
||||
var chalk = require('chalk')
|
||||
var is = require('./utils/is')
|
||||
var load = require('./utils/load')
|
||||
@ -40,7 +41,16 @@ function createApp (source, object, routes, argv) {
|
||||
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) {
|
||||
var rewriter = jsonServer.rewriter(routes)
|
||||
|
@ -1,9 +1,19 @@
|
||||
var fs = require('fs')
|
||||
var path = require('path')
|
||||
var express = require('express')
|
||||
var logger = require('morgan')
|
||||
var cors = require('cors')
|
||||
var errorhandler = require('errorhandler')
|
||||
|
||||
module.exports = function (opts) {
|
||||
var userDir = path.join(process.cwd(), 'public')
|
||||
var defaultDir = path.join(__dirname, 'public')
|
||||
var staticDir = fs.existsSync(userDir) ?
|
||||
userDir :
|
||||
defaultDir
|
||||
|
||||
opts = opts || { static: staticDir }
|
||||
|
||||
var arr = []
|
||||
|
||||
// Logger
|
||||
@ -23,11 +33,7 @@ if (process.env.NODE_ENV === 'development') {
|
||||
}
|
||||
|
||||
// Serve static files
|
||||
if (fs.existsSync(process.cwd() + '/public')) {
|
||||
arr.push(express.static(process.cwd() + '/public'))
|
||||
} else {
|
||||
arr.push(express.static(__dirname + '/public'))
|
||||
}
|
||||
arr.push(express.static(opts.static))
|
||||
|
||||
// No cache for IE
|
||||
// https://support.microsoft.com/en-us/kb/234067
|
||||
@ -38,4 +44,5 @@ arr.push(function (req, res, next) {
|
||||
next()
|
||||
})
|
||||
|
||||
module.exports = arr
|
||||
return 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 () {
|
||||
|
||||
beforeEach(function (done) {
|
||||
this.timeout(10000)
|
||||
child = cli(['http://jsonplaceholder.typicode.com/db'])
|
||||
setTimeout(done, 9000)
|
||||
this.timeout(10000)
|
||||
serverReady(PORT, 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
|
||||
// But manually updating db.json works...
|
||||
if (os.platform() === 'linux') {
|
||||
|
@ -49,7 +49,7 @@ describe('Server', function () {
|
||||
|
||||
server = jsonServer.create()
|
||||
router = jsonServer.router(db)
|
||||
server.use(jsonServer.defaults)
|
||||
server.use(jsonServer.defaults())
|
||||
server.use(jsonServer.rewriter({
|
||||
'/api/': '/',
|
||||
'/blog/posts/:id/show': '/posts/:id'
|
||||
|
@ -19,6 +19,7 @@ describe('Server', function () {
|
||||
|
||||
server = jsonServer.create()
|
||||
router = jsonServer.router(db)
|
||||
server.use(jsonServer.defaults())
|
||||
server.use(router)
|
||||
})
|
||||
|
||||
|
Reference in New Issue
Block a user