Express.js ReSTful API Mock Server
Originally developed for Reyhoon.com's development process, it's a simple mock server to remove production server dependency from client-side application development cycle. It supports GET
, POST
, PUT
, and DELETE
HTTP methods, is highly customizable, and needs minimal configuration to support new resources.
Getting Started
- Make sure you have Node.js and npm installed.
- Clone this repository:
git clone git@github.com:Ferixz/express-mock.git
- Go to project directory using
cd express-mock
- Install dependencies using
npm install
- Initialize data store files using
node seed.js
- Start the server by invoking
node mock.js
- Mock API will be available at
http://localhost:4000/
.
Adding Resources
- Add resource key to
stores
object.
// -- mock.js
var stores = {
books: []
}
- Put the respective data file inside
data
directory (e.g.data/books.json
).
// -- data/books.json
// Store files contain a single array of resource objects.
// Resource names are plural by ReST convention, so are the file names.
// It is neccessary to provide an `id` attribute for each object.
[
{
id: 1,
title: "Second Foundation",
author: "Isaac Asimov"
},
{
id: 2,
title: "Islands in the Sky",
author: "Arthur C. Clarke"
}
]
Restart the server and you will have access to books
resource using the standard ReSTful API convention:
URL | Method
:-----------:|:-----------:
books
| GET
books/:id
| GET
books
| POST
books/:id
| PUT
books/:id
| DELETE
Notes
POST
, PUT
, and DELETE
method calls will persist data in respective resource files located at data
directory. This is due the fact that I needed data persistence in an development cycle/iteration.
This behavior can be disabled by setting always_persist
option to false
.
// -- find this line in mock.js
app.set('always_persist', false);
However, a better approach would be to put your seed data inside seed
directory (with the same hierarchy as you would do with stores in data
directory) and simply copy them all over to data
directory.
Essentially, data directory is a developer/session dependent data source, which is the reason its contents are ignored from this repository.
You can either do this manually or use the tiny seed.js
script that is written for this purpose. Note that it will replace the files with the same name at the data
directory.
$> node seed.js
Keep in mind that when POST
ing data to the mock server, you don't need to provide id
attribute as it is generated by current timestamp.
// -- mock.js
router.post('/:resource', function(req, res) {
// ...
model.id = Date.now();
// ...
})
You should also mind the DELETE
method response, as it always returns success status, even if the model does not exist!
patch
?
What happened to I believe PATCH
method lands on the wrong side of state transfer philosophy of ReST. I omitted PATCH
method from almost every recent APIs I worked on (or just mapped them to PUT
actions), and benefited from more stable and bug free API calls.
Special Resource Handlers
If you need to provide a different behavior for some resources, add the route handlers before default ReST handlers.
// -- Somewhere in the mock.js file you'll find these lines
...
// Put special resource handlres HERE
// ...
// End of special resource handlers
// ===================================
// Default ReST Handlers
router.post('/:resource', function(req, res) {
let resource = req.params.resource;
...
The MIT License (MIT)
Copyright (c) 2016 Farzad
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.