json-server test drive

Overview

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.

Project Resources

Scripts

  • run static db file with json-server db.json or npm run start
  • run generated db file with json-server generate.js or npm run faker for faker people
  • run modularized json-server db (for adding existing projects) with node ./existing-project server.js or npm run module

Commands

  • type s in terminal to save db snapshot
  • curl to GET curl http://localhost:3333/people

Config

  • .json-server.json optional, or options can be set in npm scripts

Notes

  • specify port with -p
  • optional specification of db.json path ./db.json or db.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

Querying

  • Add query strings according to data: http://localhost:3333/people?first_name=Melisandra
  • Search all properties with q : http://localhost:3333/people?q=Steve

Auto-generated Endpoints

  • GET /people
  • GET /people/{id}
  • POST /people
  • PUT /people/{id}
  • PATCH /people/{id}
  • DELETE /people/{id}

Add to existing project as a module

  • 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",

Other Resources

  • 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 **