Add static option

This commit is contained in:
Typicode
2015-09-19 10:48:56 +02:00
parent 6610adbf05
commit 6c6333fa6d
9 changed files with 97 additions and 37 deletions

View File

@ -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

View File

@ -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')

View File

@ -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)'

View File

@ -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)

View File

@ -1,41 +1,48 @@
var fs = require('fs')
var path = require('path')
var express = require('express')
var logger = require('morgan')
var cors = require('cors')
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
arr.push(logger('dev', {
opts = opts || { static: staticDir }
var arr = []
// 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 }))
// Enable CORS for all the requests, including static files
arr.push(cors({ origin: true, credentials: true }))
if (process.env.NODE_ENV === 'development') {
if (process.env.NODE_ENV === 'development') {
// only use in development
arr.push(errorhandler())
}
}
// Serve static files
if (fs.existsSync(process.cwd() + '/public')) {
arr.push(express.static(process.cwd() + '/public'))
} else {
arr.push(express.static(__dirname + '/public'))
}
// Serve static files
arr.push(express.static(opts.static))
// No cache for IE
// https://support.microsoft.com/en-us/kb/234067
arr.push(function (req, res, next) {
// 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
return arr
}

View File

@ -0,0 +1 @@
Hello

View File

@ -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') {

View File

@ -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'

View File

@ -19,6 +19,7 @@ describe('Server', function () {
server = jsonServer.create()
router = jsonServer.router(db)
server.use(jsonServer.defaults())
server.use(router)
})