json-server
Advanced tools
Comparing version 0.7.17 to 0.7.18
{ | ||
"name": "json-server", | ||
"version": "0.7.17", | ||
"version": "0.7.18", | ||
"description": "Serves JSON files through REST routes.", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -9,2 +9,4 @@ # JSON Server [![](https://travis-ci.org/typicode/json-server.svg)](https://travis-ci.org/typicode/json-server) [![](https://badge.fury.io/js/json-server.svg)](http://badge.fury.io/js/json-server) [![](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/typicode/json-server?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
* [JSONPlaceholder - Live running version](http://jsonplaceholder.typicode.com) | ||
_See also [hotel](https://github.com/typicode/hotel), a simple server manager._ | ||
@@ -38,7 +40,7 @@ ## Example | ||
Also, if you make POST, PUT, PATCH or DELETE requests, changes will be automatically saved to `db.json` using [lowdb](https://github.com/typicode/lowdb). | ||
Also, if you make POST, PUT, PATCH or DELETE requests, changes will be automatically and safely saved to `db.json` using [lowdb](https://github.com/typicode/lowdb). | ||
## Routes | ||
Based on the previous `db.json` file, here are all the default routes. You can also add [other routes](#add-routes). | ||
Based on the previous `db.json` file, here are all the default routes. You can also add [other routes](#add-routes) using `--routes`. | ||
@@ -154,3 +156,3 @@ ``` | ||
"/api/": "/", | ||
"/blog/posts/:id/show": "/posts/:id" | ||
"/blog/:resource/:id/show": "/:resource/:id" | ||
} | ||
@@ -171,3 +173,2 @@ ``` | ||
/blog/posts/1/show | ||
# ... | ||
``` | ||
@@ -198,9 +199,20 @@ | ||
To add rewrite rules: | ||
To modify responses, use `router.render()`: | ||
```javascript | ||
// In this example, returned resources will be wrapped in a body property | ||
router.render = function (req, res) { | ||
res.jsonp({ | ||
body: res.locals.data | ||
}) | ||
} | ||
``` | ||
To add rewrite rules, use `jsonServer.rewriter()`: | ||
```javascript | ||
// Add this before server.use(router) | ||
server.use(jsonServer.rewriter({ | ||
'/api/': '/', | ||
'/blog/posts/:id': '/posts/:id' | ||
'/blog/:resource/:id/show: '/:resource/:id' | ||
}) | ||
@@ -207,0 +219,0 @@ ``` |
@@ -14,2 +14,10 @@ var fs = require('fs') | ||
// 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()) | ||
} | ||
// Serve static files | ||
@@ -22,10 +30,2 @@ if (fs.existsSync(process.cwd() + '/public')) { | ||
// CORS | ||
arr.push(cors({ origin: true, credentials: true })) | ||
if (process.env.NODE_ENV === 'development') { | ||
// only use in development | ||
arr.push(errorhandler()) | ||
} | ||
// No cache for IE | ||
@@ -32,0 +32,0 @@ // https://support.microsoft.com/en-us/kb/234067 |
@@ -117,3 +117,8 @@ var express = require('express') | ||
} else { | ||
array = db(req.params.resource).filter(filters) | ||
var chain = db(req.params.resource).chain() | ||
for (var f in filters) { | ||
// This syntax allow for deep filtering using lodash (i.e. a.b.c[0]) | ||
chain = chain.filter(f, filters[f]) | ||
} | ||
array = chain.value() | ||
} | ||
@@ -120,0 +125,0 @@ } |
@@ -39,2 +39,7 @@ var request = require('supertest') | ||
db.deep = [ | ||
{ a: { b: 1 } }, | ||
{ a: 1 } | ||
] | ||
server = jsonServer.create() | ||
@@ -87,2 +92,10 @@ router = jsonServer.router(db) | ||
}) | ||
it('should support deep filter', function (done) { | ||
request(server) | ||
.get('/deep?a.b=1') | ||
.expect('Content-Type', /json/) | ||
.expect([db.deep[0]]) | ||
.expect(200, done) | ||
}) | ||
}) | ||
@@ -89,0 +102,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39953
958
249