JSON Server

JSON Server is a zero coding API that you can spin up and configure in less than a minute. Its probably more for front end developers that need a quick API to test but I’ve used it as a mock API when the existing mocks were either broken or too complicated to setup.

An alternative package is Minimal API InstantAPIs.

Setup

  1. Create a local folder, example C:\data\devops\json-server

  2. Install json-server

1
2
cd json-server
npm install --save-dev json-server

It does pull down over a hundred node_modules - who knows what vulnerabilities these will bring :D

  1. Create the database as db.json and populate with json data. It supports auto incrementing id’s and UUIDs. It also supports complex objects like users.

Example data below is from one of my many unfinished projects -_-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{
"schedule": [{
"id": 1,
"equipment": "gum extruder",
"jobDescription": "Check the sensor for no gum is working",
"startDate": "2018-04-25",
"nextDate": "2018-07-25"
},{
"id": 2,
"equipment": "gum extruder",
"jobDescription": "Check the heat exchager temperature, that between 50 and 55 degrees Celsius.",
"startDate": "2018-04-26",
"nextDate": "2018-07-26"
}],
"interval": [{
"id": 1,
"name": "Daily"
},{
"id": 2,
"name": "Weekly"
},{
"id": 3,
"name": "Monthly"
}],
"users": [{
"id":"5a4e6973-9e7d-4033-bf5b-e16a7a18d264",
"firstName": "foo",
"lastName": "bar",
"email": "foo.bar@gmail.com",
"phoneNumbers": [
{
"country": "New Zealand",
"number": "000 000 000"
}]
}
]
}
  1. If you adding this to an existing project that uses npm you can just add to the package.json file under scripts. If you dont have a file and want to run it as a stub for something then just create the file as below. Alternatively you can run npm init, enter through all the steps and it will create a file for you where you can add this script.
1
2
3
4
5
{
"scripts": {
"api": "json-server db.json -p 3333 --delay 1500"
}
}
  1. Run the server using the shorthand script you just created
1
npm run api
  1. The data will be avalible at restful endpoints

GET

POST

When posting from postman set the header value Content-Type : application/json

References