connect-mock-rest
v1.3.0
Published
Connect middleware to mock rest endpoints with json files
Downloads
2
Readme
connect-mock-rest
Mock rest API middleware for connect. It turns any json file in to a rest API.
When using this middleware GET
, POST
, PUT
, PATCH
and DELETE
can be performed on any json file. This is useful for rapidly prototyping an application using only json files as endpoints.
This module works particularly nice when combined with my fork of TJ's serve which can be found here.
Setup
$ npm install body-parser
$ npm install connect-mock-rest
Usage
var path = require('path')
, connect = require('connect')
, mockRest = require('connect-mock-rest')
, bodyParser = require('body-parser');
var server = connect();
server.use(bodyParser());
server.use(mockRest());
If you are serving from a different location you need to specify that:
server.use(mockRest(path.join(__dirname, 'public')));
Create a json file containing an array. In this example /foo.json
contains []
. Start your server and then:
$ curl http://localhost:3000/foo.json
[]
$ curl -H "Content-Type: application/json" -X POST -d '{"name":"steve"}' http://localhost:3000/foo.json
{"name":"steve","id":"29b6ae06-87ec-422c-9efc-ad3c7e759364"}
$ curl http://localhost:3000/foo.json
[{"name":"steve","id":"29b6ae06-87ec-422c-9efc-ad3c7e759364"}]
$ curl -H "Content-Type: application/json" -X PUT -d '{"name":"foo"}' http://localhost:3000/foo.json/29b6ae06-87ec-422c-9efc-ad3c7e759364
{"name":"foo","id":"29b6ae06-87ec-422c-9efc-ad3c7e759364"}
$ curl http://localhost:3000/foo.json
[{"name":"foo","id":"29b6ae06-87ec-422c-9efc-ad3c7e759364"}]
$ curl -H "Content-Type: application/json" -X PATCH -d '{"name":"bar"}' http://localhost:3000/foo.json/29b6ae06-87ec-422c-9efc-ad3c7e759364
{"name":"bar","id":"29b6ae06-87ec-422c-9efc-ad3c7e759364"}
$ curl http://localhost:3000/foo.json
[{"name":"bar","id":"29b6ae06-87ec-422c-9efc-ad3c7e759364"}]
$ curl http://localhost:3000/foo.json/29b6ae06-87ec-422c-9efc-ad3c7e759364
{"name":"bar","id":"29b6ae06-87ec-422c-9efc-ad3c7e759364"}
$ curl -X DELETE http://localhost:3000/foo.json/29b6ae06-87ec-422c-9efc-ad3c7e759364
{"name":"bar","id":"29b6ae06-87ec-422c-9efc-ad3c7e759364"}
$ curl http://localhost:3000/foo.json
[]
Things to note
- This is intended for rapid prototyping purposes only. You would be mad to use this on any real project.
- Objects are created with a GUID which is stored as
id
. Any existingid
will be overwritten. - Resource nesting is not supported.
- Security is not considered at all.
- Invalid requests will not be handled well. This thing is far from bulletproof.
- There is no validation of incoming data.
- Duplicate objects will be created if the same object is posted twice.
- When finding items the first match is returned. This applies to all methods.