Running a JSON Server in the Terminal using Process Manager (PM2): A Step-by-Step Guide

Running a JSON Server in the Terminal using Process Manager (PM2): A Step-by-Step Guide

Introduction

Process manager (PM2) is a tool that is used to manage and monitor processes that are running on a system. It allows developers to start, stop, monitor, and automatically restart processes when needed. It is mostly used when working with server applications that should be kept running continuously.

A JSON server allows us to create API endpoints that can be used during testing or development then there is no available backend server. In simple terms, it allows us to create a RESTful API using a simple JSON file.

In this step-by-step guide, we will explain the process of running a JSON server in the terminal using Process Manager(PM2). Let's get started.

Prerequisite

Make sure you have Node.js and NPM installed on your computer. You can check if they are installed by running these commands in your terminal:

node -v
npm -v

If you don't see the version numbers, you'll need to download and install Node.js from the official website.

How to run a JSON Server in the Terminal using Process Manager

STEP 1: Install JSON Server and PM2

Open your terminal or command prompt and install both json-server and pm2 globally using npm.

npm install json-server
npm install pm2@latest -g

STEP 2: Create a JSON file

Create a JSON file named db.json within a folder named data. This JSON file will function as your database for the JSON Server.

data/
│  db.json/

The data inside your db.json file should be formatted this way:

{
  "blogs": [
 { 
"id": 1, 
"title": "Fundamentals of JavaScript", 
"author": "John Doe" 
},
{ 
"id": 2, 
"title": "Props drilling in React", 
"author": "Jane Smith"
}
 ]
}

STEP 3: Create a server.js file

Create the server.js file in the project directory. This file will contain the script to start the JSON server. In the server.js file, add the following code:


import pkg from 'json-server';
const { create, router: _router, defaults } = pkg;

const server = create();
const router = _router('./data/db.json');
const middlewares = defaults();

server.use(middlewares);
server.use(router);

const PORT = 3000; // Change the port if needed

server.listen(PORT, () => {
  console.log(`JSON Server is running on port ${PORT}`);
});

STEP 4: Start the JSON Server with PM2

In your terminal, navigate to the directory containing the db.json file, and start the JSON Server using PM2:

pm2 start server.js --watch ./data/db.json --name json-server

In the above command, pm2 start server.js starts the JSON server script (server.js) using pm2. --watch db.json tells JSON Server to watch the db.json file for changes and update the data accordingly. --name json-server sets the name of the PM2 process as json-server.

Start-json-server-with-pm2

The JSON server will now run continuously in the background, managed by PM2.

STEP 5: Access the JSON Server

To see the list of running processes managed by PM2, use the following command:

pm2 list

pm2-list

You can access your JSON Server's endpoints at http://localhost:3000/blogs in your browser.

To check the status or manage the JSON server process, you can use various pm2 commands. Here are some examples:

To save the JSON server process:

pm2 save json-server

The pm2 save command in PM2 is used to save the currently running processes into a process file. This process file is known as the PM2 ecosystem file or ecosystem.config.js by default.

To stop the JSON server process:

pm2 stop json-server

To start the JSON server process if it's stopped:

pm2 start json-server

To restart the JSON server process:

pm2 restart json-server

To delete the JSON server process from pm2:

pm2 delete json-server

This will stop the JSON Server and remove it from PM2's process list.

Resources

JSON-Server Documentation

Process Manager Documentation

Conclusion

In conclusion, process manager (PM2) makes it easy to manage server processes, especially in Node.js applications. When used alongside the JSON Server, it allows developers to set up a mock API server using a simple JSON file, providing a way to test frontend applications without the need for a fully functional backend server. By following the step-by-step guide provided above, you can effortlessly run a JSON Server using PM2.