This is a test drive of the json-server project. It is a simple way to add a quick REST backend and db to a project. Adding to a project as a module makes the most sense - see Add to existing project as a module
below.
- json-server for super fast mock BE, endpoints
- Mockaroo for hard-coded mock data
- Faker for dynamic mock data
- run static db file with
json-server db.json
ornpm run start
- run generated db file with
json-server generate.js
ornpm run faker
for faker people - run modularized json-server db (for adding existing projects) with
node ./existing-project server.js
ornpm run module
- type
s
in terminal to save db snapshot - curl to GET
curl http://localhost:3333/people
.json-server.json
optional, or options can be set in npm scripts
- specify port with
-p
- optional specification of db.json path
./db.json
ordb.json
- paginate with
/people?_page=2
,/people?_page=2&_limit=20
(limit default at 10) - sort with
/people?_order=desc,asc
for more options, see json-server
- Add query strings according to data:
http://localhost:3333/people?first_name=Melisandra
- Search all properties with
q
:http://localhost:3333/people?q=Steve
- GET
/people
- GET
/people/{id}
- POST
/people
- PUT
/people/{id}
- PATCH
/people/{id}
- DELETE
/people/{id}
- install with
npm i json-server --save-dev
- create
server.js
file, for example:
// server.js
const jsonServer = require("json-server");
const path = require("path");
const server = jsonServer.create();
const router = jsonServer.router(path.join(__dirname, "db.json"));
const middlewares = jsonServer.defaults();
const PORT = 4444;
server.use(middlewares);
server.use(router);
server.listen(PORT, () => {
console.log(`Module JSON Server listening on port ${PORT}`);
});
- Add script to start
"module": "node ./existing-project/server.js",
- JSON Placeholder for free fake API
- My JSON Server to create db.json on GitHub, just create a repo with db.json file only
- Hotel for easy project spin-up management, start projects from the browser ** Doesn't work with Node 14 **