Get A Dummy REST Service With json-server Npm Package, Less Than One Minute

Tugce Konuklar Dogantekin
3 min readMay 7, 2021

--

Sometimes we need to have a simple backend services just to test some solutions in our development environment or for some prototypes that we would like to run quickly. Here in this article I will give some information how you can achieve to build a simple Rest backend service with a tiny npm package, which is called json-server.

Json Server is a npm package, that is basicly allows you to create a local dummy Rest Api by behave as a backend server for development. This package help us to have a backend api by saving us to setup a big backend solution with database and endpoints.

Json server just requires a simple Json file to store our data and wraps json data file with some api endpoints to interact with the data.

Json Server provides some GET, POST, PATCH and DELETE endpoints and some other operations such as pagination and searching by warping json data file. Here you can get more in detail information and documentation.

Let’s start to build server;

1- Instal json-server npm package :

npm install -g json-server

2- Create a db.json file and add your dummy data in json format like below.

{
"users": [
{
"id": 1,
"name": "John Doe",
"age": 28,
"postId": [
1,
2
]
},
{
"id": 2,
"name": "Jane Doe",
"age": 25,
"postId": 3
}
],
"posts": [
{
"id": 1,
"title": "Getting started json-server",
"detail": "json server is cool"
},
{
"id": 2,
"title": "Simple Server",
"detail": "json server is easy"
},
{
"id": 3,
"title": "Hello World",
"detail": "greeting from Mars"
}
]
}

3- Run the command below in terminal with in the same path of db.json file

if you use a node project you can simply create a new script in your package.json file just to start the server

json-server --watch db.json

4- Your server is ready, you can go to http://localhost:3000/ and done!

As you see that /posts, and /users resources endpoints are ready for you.

In my Github repo I create a node app , add json server as dependency and to make it easy create a npm script to run server.

What can you do more with this server ?

This package provides some useful operations such as pagination, sorting, filtering, searching etc.

Here I would like to list some common usage samples

  1. json server generates GET, POST, PUT, PATCH and DELETE endpoints for each resource
GET /users
GET /users/1
POST /users
PUT /users/1
PATCH /users/1
DELETE /users/1

2. Creates relation between your resources by looking your data source, In my example, creates relation between users and posts wit postId attribute in user resource.

GET /users/1/posts

3. Filtering & Searching

Server is also provide some filtering actions. You can also make text search by using q tag. and also you can use _like operator to filter (RegExp supported)

GET /posts?title=Simple%20Server&detail=json%20server%20is%20easy
GET /posts?q=json%20server
GET /posts?title_like=server

4. Pagination

GET /users?_page=1&_limit=10

5. Sorting

/users?_sort=age&_order=asc

6. Operators

There are some types operators _gte and_lte , _ne to exclude a value,

GET /users?age_gte=20&age_lte=30
GET /users?id_ne=1

And some more usages you can see in documentation

What ever you do with endpoints you can see the changes in your data source json file. Open your db.json file you will see your db informations were overwritten with your new information.

In this article I would like to give some quick information how easy to create a dummy rest server, I strongly suggest to you to play and read its documentation, you will love to use in your projects.

Simply I use json server as a node project like this, you can find from my Github repo.

I would like to thank to Didem Kucukkaraaslan for producing quality content on this subject.

See you in the next article.

--

--

No responses yet