diff --git a/CHANGELOG.md b/CHANGELOG.md index 059d64d..9624197 100644 --- a/CHANGELOG.md +++ b/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 diff --git a/README.md b/README.md index fcc5f60..57589e7 100644 --- a/README.md +++ b/README.md @@ -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') diff --git a/src/cli/index.js b/src/cli/index.js index 6609324..a7b44b4 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -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)' diff --git a/src/cli/run.js b/src/cli/run.js index 4a930a3..2af560a 100644 --- a/src/cli/run.js +++ b/src/cli/run.js @@ -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) diff --git a/src/server/defaults.js b/src/server/defaults.js index 633914c..3b1646e 100644 --- a/src/server/defaults.js +++ b/src/server/defaults.js @@ -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', { - skip: function (req, res) { - return process.env.NODE_ENV === 'test' || - req.path === '/favicon.ico' + 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 })) + + if (process.env.NODE_ENV === 'development') { + // only use in development + arr.push(errorhandler()) } -})) -// Enable CORS for all the requests, including static files -arr.push(cors({ origin: true, credentials: true })) + // Serve static files + arr.push(express.static(opts.static)) -if (process.env.NODE_ENV === 'development') { - // only use in development - arr.push(errorhandler()) + // 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() + }) + + 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 diff --git a/test/cli/fixtures/public/index.html b/test/cli/fixtures/public/index.html new file mode 100644 index 0000000..e965047 --- /dev/null +++ b/test/cli/fixtures/public/index.html @@ -0,0 +1 @@ +Hello diff --git a/test/cli/index.js b/test/cli/index.js index a9b6161..c8c9683 100644 --- a/test/cli/index.js +++ b/test/cli/index.js @@ -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') { diff --git a/test/server/plural.js b/test/server/plural.js index f0c3806..0585d02 100644 --- a/test/server/plural.js +++ b/test/server/plural.js @@ -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' diff --git a/test/server/singular.js b/test/server/singular.js index 5314165..058f75c 100644 --- a/test/server/singular.js +++ b/test/server/singular.js @@ -19,6 +19,7 @@ describe('Server', function () { server = jsonServer.create() router = jsonServer.router(db) + server.use(jsonServer.defaults()) server.use(router) })