As web developers, we often find ourselves dealing with complex web applications that require continuous monitoring and management of server processes. One such popular process manager in the Node.js ecosystem is pm2. In this article, we will explore how to use pm2 to set up an Express server and React app.
What is pm2?
pm2 is a process manager for Node.js applications that allows us to keep our applications running continuously, even if they encounter errors or crashes. It is an open-source project and can be installed easily using npm. pm2 is designed to provide a simple, reliable, and powerful process manager for Node.js applications.
Setting up Express server using pm2
Express is a popular web framework for building web applications in Node.js. Let's see how to set up an Express server using pm2.
First, create a new Express application using the following command:
npm install express-generator -g
express myapp
Once the application is created, navigate to the root directory of the application and install the dependencies using the following command:
cd myapp
npm install
Now, let's install pm2 globally using the following command:
npm install pm2 -g
Once pm2 is installed, start the Express server using pm2 with the following command:
pm2 start ./bin/www --name myapp
This command starts the Express server using the bin/www file and names the process as myapp. You can check the status of the process using the following command:
pm2 status
This will show you the status of all the processes managed by pm2, including the Express server process.
Setting up React app using pm2
Now that we have set up an Express server using pm2, let's see how to set up a React app using pm2.
First, create a new React app using the following command:
npx create-react-app myapp
Once the application is created, navigate to the root directory of the application and install the dependencies using the following command:
cd myapp
npm install
Now, let's build the React app using the following command:
npm run build
Once the app is built, we can serve it using a static file server such as serve. Let's install serve globally using the following command:
npm install serve -g
Now, serve the React app using the following command:
serve -s build -l 3000
This serves the React app on port 3000. You can check the app by visiting http://localhost:3000 in your browser.
Finally, we can use pm2 to manage the static file server process. Start the server process using the following command:
pm2 serve build 3000 --name myapp
This command starts the static file server on port 3000 using the build directory and names the process as myapp. You can check the status of the process using the following command:
pm2 status
Conclusion
In this article, we have seen how to use pm2 to manage both an Express server and a React app. pm2 provides a powerful and easy-to-use process manager for Node.js applications, allowing us to keep our applications running continuously and reliably. With pm2, we can easily manage complex web applications and ensure that they run smoothly.