Creating a Local API Server using JSON Server

Anurag Nema
3 min readJul 16, 2021

--

Before doing so, Ensure that Node is installed on your system, if not then install it from https://nodejs.org/en/download. After this, you can run the following command on git bash or Command Prompt to install json server :

npm install -g json-server
Installed json-server using git bash

As of now, our json-server installed successfully, we need to start our local server, for doing so run the command below:

json-server --watch db.json
Started json-server on port 3000

Now as our local server started, we can see in our git bash or cmd our server is running on http://localhost:3000 by default,but if you want to change the port (say 8000) use the command below:

json-server --watch db.json --port 8000
json-server running in http://localhost:3000

Now let’s check the db.json file that is saved on the system locally, it will be on the same location you started json-server.

db.json on file location where you started json-server

When we open db.json file we get data related to posts, comments, profiles as we have seen on http://localhost:3000 like:

db.json

you can also see the above data by clicking on link in http://localhost:3000/posts, http://localhost:3000/comments, http://localhost:3000/profile.

Now comes interesting work, lets create our own data on the local server.

For that let’s delete data on the db.json file and put our own like:

Edited data (our own data) of db.json

Now refresh http://localhost:3000,you will see Resources section will be updated according to db.json (edited) as :

http://localhost:3000 (updated)

Now you can watch your data on clicking links http://localhost:3000/Books, http://localhost:3000/Movies or whatever is your data.

http://localhost:3000/Books
http://localhost:3000/Movies

On localhost we can see specific data using their id, for example, http://localhost:3000/Books/2 will give data about whose id is 2 as shown below:

http://localhost:3000/Books/2

It was my first article, I will try to be better every time and I hope you like that, thanks for reading !! It Let’s meet with the next article ❤️

--

--

Anurag Nema

I am a Student, pursuing B.Tech in Computer Science Engineering. All I want is to learn,connect and Share.