Mock REST Server
No configuration needed: Just start it, and test your code!
Mock REST Server comes with these features 🚀 :
- Support GET, POST, PUT, PATCH, DELETE and OPTIONS HTTP methods
- Accept and respond with JSON
- Support real HTTP status code response from REST API
- Return HTTP error status codes via api version
- Fake latency
- Filtering, sorting and pagination
- Auto populate tool to fill database, don't waste your time for a REST server!
Changes
All details of changes to this project will be documented in this file.
Installation & Usage
npm i -D mock-rest-servernode_modules/.bin/mock-rest-server
Param name | Description | Type | Default |
---|---|---|---|
--port=3000 |
(Optional) Change server port | Number |
3000 |
--silent |
(Optional) Hide server output | Boolean |
false |
--latency |
(Optional) wait before response | Number |
0 |
Unit test
For more details, look at the full example from test file.
Assuming you're using a module-compatible system (like webpack), start MockRestServer on top of your unit test file:
MockRestServer come with (optional) awesome feature that fill database with random typed data: populate(collection, length, schema)
Param name | Description | Type |
---|---|---|
collection |
Collection name to create/populate | String |
length |
Number of resource to create in collection | Number |
schema |
Object send to create a resource. Values are replaced by random value based on JS type String , Number , Date or Boolean . |
Object |
Example
Start MockRestServer, open a new shell and run some curl on api /v1/
:
curl -X POST -d '{"title":"Awesome news!","body":"Some content."}' http://localhost:3000/v1/articles# {"title":"Awesome news!","body":"Some content.","id":1}
Now, get your articles with:
curl http://localhost:3000/v1/articles# [{"title":"Awesome news!","body":"Some content.","id":1},{"title":"Awesome news!","body":"Some content.","id":2}] curl http://localhost:3000/v1/articles/1# {"title":"Awesome news!","body":"Some content.","id":1}
Use api /v[xxx]/
to mock HTTP status codes (403, 404, 500...) from server response:
curl http://localhost:3000/v403/articles
Contribution
Any help or feedback are really welcome, no matter how great or small!
Please make sure to read the Contributing Guide before making a pull request.