Creating a Local API Server using JSON Server
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
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
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
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.
When we open db.json file we get data related to posts, comments, profiles as we have seen on http://localhost:3000 like:
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:
Now refresh http://localhost:3000,you will see Resources section will be updated according to db.json (edited) as :
Now you can watch your data on clicking links http://localhost:3000/Books, http://localhost:3000/Movies or whatever is your data.
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:
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 ❤️