Create a mock Restful API using JSON Server, which saves entities to JSON files
npm-run-all
allows run multiple processes in parallelnpm install --save json-server npm-run-all
jsonServer.js
:const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json'); //File where the objects will be saved
const middlewares = jsonServer.defaults();
server.use(function(req, res, next) {
setTimeout(next, 1000); //timeout to get the responses
});
server.use(middlewares);
server.use('/rest', router); // The endpoint will be http://[host]:[port]/rest/[entity]
server.listen(4000, () => {
console.log('JSON server is running on port 4000');
});
db.json
with the initial entities:{
"speakers": [
{
"id": "45454",
"firstName": "Gabriel"
},
{
"id": "5443",
"firstName": "Mark"
}
],
"sessions": [
{
"id": 6182,
"title": "Diving INTO THE Deep Learning",
"sessionUrl": "diving-into-deep-learning"
}
]
}
packages.json
create the following NPM scripts:"scripts": {
"start:client": "webpack-dev-server --hot --config webpack.client.js",
"jsonserver": "node jsonserver js",
"start": "npm-run-all --parallel start:client jsonserver"
}
npm run start