A short step by step by step guide to creating a JSON-DB server and deploying it on render
json-server is a tool for creating mock REST API fast! To get started, ensure you have the following requirements:
NodeJS (npm)
Let's get started!
On an empty folder, initiate a nodejs application by running the following on your terminal/CMD:
npm init -y
Once that is complete, you install the following packages:
`
json-server
json-serve
cors
`
npm install json-server json-serve cors
After the installation, create a new file: index.js. This is the entry point for the json-serve. Add the following inside the file:
`
const jsonServer = require("json-server"); // importing json-server library
const server = jsonServer.create();
const router = jsonServer.router("db.json");
const middlewares = jsonServer.defaults();
const port = process.env.PORT || 3001; // you can use any port number here; i chose to use 3001
server.use(middlewares);
server.use(router);
server.listen(port);
`
In the code above, a server has been created that will be fetching and updating data from a json file, db.json
In the project root, create a new file: db.json and add the following:
{
"feedback": [
{
"id": 1,
"rating": 10,
"user_name": "Tony Stark",
"text": "You are the ironman of this world"
},
{
"id": 2,
"rating": 9,
"user_name": "Bruce Wayne",
"text": "You are the batman of this world"
},
{
"id": 3,
"rating": 8,
"user_name": "Peter Parker",
"text": "You are the spiderman of this world"
}
]
}
The mock server is ready to run, but let's add some scripts in package.json:
Update the "scripts" to:
"scripts": {
"start": "node index.js"
},
Deploy On Render :-
Go To Site :- https://dashboard.render.com/
Select Web Services
Connect with Github
Go to Github create new repo
Clone this repo and create all files and installation of json-server all are mentioned in above.
push all the code on Github
Go To Render And Select Github Repo where push all json-server code
Click on create Web Service Button below
and wait few minutes for progress and enjoy
Hopefully this article useful for you , happy coding! || Like this article ♥
Top comments (0)