JSONServer
Small JSON file server for REST API mock
Installation
$ npm install -g jsonserver
Usage
Run server:
$ jsonserver --path path/to/file.json
Change default server port:
$ jsonserver --path path/to/file.json --port 5000
Entry points
Return an array of items in a resource
GET /:resource
curl -i -H "Accept: application/json" -X GET http://localhost:3000/movies HTTP/1.1 200 OKX-Powered-By: ExpressAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Headers: Content-TypeAccess-Control-Allow-Methods: GET, PUT, POST, DELETEAccess-Control-Expose-Headers: *Access-Control-Allow-Origin: *Content-Type: application/jsonContent-Length: 392ETag: "-1191736009"Date: Sat, 31 May 2014 17:51:29 GMTConnection: keep-alive [{"title":"Die Hard","genre":"Action","director":"John McTiernan","description":"John McClane, officer of the NYPD, tries to save...","id":"e757cd3b-caba-3f1c-a4ef-dc409691a820"},{"title":"Transporter","genre":"Action","director":"Corey Yuen","description":"This film is about a man whose job is to deliver packages without asking any questions.","id":"1a6d970e-abd8-8b44-50b1-9714d2525c4b"}]
Return an array of match items in a resource
GET /search/:resource
curl -i -H "Accept: application/json" -X GET http://localhost:3000/search/movies?title="Transporter" HTTP/1.1 200 OKX-Powered-By: ExpressAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Headers: Content-TypeAccess-Control-Allow-Methods: GET, PUT, POST, DELETEAccess-Control-Expose-Headers: *Access-Control-Allow-Origin: *Content-Type: application/jsonContent-Length: 214ETag: "-29677262"Date: Sat, 31 May 2014 17:54:21 GMTConnection: keep-alive [{"title":"Transporter","genre":"Action","director":"Corey Yuen","description":"This film is about a man whose job is to deliver packages without asking any questions.","id":"1a6d970e-abd8-8b44-50b1-9714d2525c4b"}]
Return a resource object matching with the ID
GET /:resource/:id
curl -i -H "Accept: application/json" -X GET http://localhost:3000/movies/1a6d970e-abd8-8b44-50b1-9714d2525c4b HTTP/1.1 200 OKX-Powered-By: ExpressAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Headers: Content-TypeAccess-Control-Allow-Methods: GET, PUT, POST, DELETEAccess-Control-Expose-Headers: *Access-Control-Allow-Origin: *Content-Type: application/jsonContent-Length: 212ETag: "-2005851238"Date: Sat, 31 May 2014 17:52:24 GMTConnection: keep-alive {"title":"Transporter","genre":"Action","director":"Corey Yuen","description":"This film is about a man whose job is to deliver packages without asking any questions.","id":"1a6d970e-abd8-8b44-50b1-9714d2525c4b"}
Create a new object on a resource
POST /:resource
curl -i -H "Accept: application/json" -H "Content-type: application/json" -d '{"title": "Die Hard", "genre": "Action", "director": "John McTiernan", "description": "John McClane, officer of the NYPD, tries to save..."}' -X POST http://localhost:3000/movies HTTP/1.1 200 OKX-Powered-By: ExpressAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Headers: Content-TypeAccess-Control-Allow-Methods: GET, PUT, POST, DELETEAccess-Control-Expose-Headers: *Access-Control-Allow-Origin: *Content-Type: application/jsonContent-Length: 177Date: Sat, 31 May 2014 17:48:59 GMTConnection: keep-alive {"title":"Die Hard","genre":"Action","director":"John McTiernan","description":"John McClane, officer of the NYPD, tries to save...","id":"e757cd3b-caba-3f1c-a4ef-dc409691a820"}
Updated a resource object giving the ID
PUT /:resource/:id
curl -i -H "Accept: application/json" -H "Content-type: application/json" -d '{"title": "Transporter 2"}' -X PUT http://localhost:3000/movies/1a6d970e-abd8-8b44-50b1-9714d2525c4b HTTP/1.1 200 OKX-Powered-By: ExpressAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Headers: Content-TypeAccess-Control-Allow-Methods: GET, PUT, POST, DELETEAccess-Control-Expose-Headers: *Access-Control-Allow-Origin: *Content-Type: application/jsonContent-Length: 214Date: Sat, 31 May 2014 17:58:41 GMTConnection: keep-alive {"title":"Transporter 2","genre":"Action","director":"Corey Yuen","description":"This film is about a man whose job is to deliver packages without asking any questions.","id":"1a6d970e-abd8-8b44-50b1-9714d2525c4b"}
Deleted a resource object giving the ID
DELETE /:resource/:id
curl -i -H "Accept: application/json" -X DELETE http://localhost:3000/movies/1a6d970e-abd8-8b44-50b1-9714d2525c4b HTTP/1.1 200 OKX-Powered-By: ExpressAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Headers: Content-TypeAccess-Control-Allow-Methods: GET, PUT, POST, DELETEAccess-Control-Expose-Headers: *Access-Control-Allow-Origin: *Content-Type: application/jsonContent-Length: 2Date: Sat, 31 May 2014 17:59:23 GMTConnection: keep-alive true