Nodejs automatically restarts after modifying the code
NodeJs can automatically restart after modifying the code, saving us the trouble of ctr+c and then using node. But in terms of time, if the project is already online and running normally, and there are not many modifications, then we can do it manually. But if it is in the process of development, every time you finish writing the code, you need to manually stop the service and then restart the service, which is really quite troublesome.
For more information about NodeJs, please refer to our NodeJs tutorial
As a programmer, this kind of repetitiveness can be annoying.
Now here I will introduce several ways to make Nodejs restart automatically, saving you the trouble of manual restart.
Forever implements NodeJs automatic restart
Forever is a client tool that can be used to keep the NodeJs service running in the background. Similarly, it can also detect file changes and automatically restart the NodeJs service.
Forever is installed as follows
$ npm install forever –g
Run the above command, wait for forever to be installed, and then we can use forever.
$ forever start app.js
The above command can start the NodeJs service, and the NodeJs service is already running in the backend. Isn't it very simple? Of course, the above command is the most basic to start the NodeJs service. But we can't view the print information just by running the above command. Therefore, if we need to view the print information, we can output this information to the log file. The command is as follows
$ forever –l /log/forever.log –o /log/out.log –e /log/error.log app.js
In this way, we can view the information printed by using console.log() and other functions in the log file.
The last step is to automatically restart the service after modifying the NodeJs code. To do this, use the -w option.
$ forever –l /log/forever.log –o /log/out.log –e /log/error.log –w app.js
In this way, we can achieve our ultimate goal.
pm2 implements automatic restart of NodeJs
Similarly, like forever , pm2 is also a client tool that can also make the NodeJs service run in the background. It can also detect file changes and automatically restart the NodeJs service.
The pm2 installation method is as follows
$ npm install pm2@latest –g
After running the above command, wait for pm2 installation to complete.
$ pm2 start app.js
Start the NodeJs service.
$ pm2 start –l /log/pm2.log –o /log/out.log –e /log/error.log app.js
Write the NodeJs log, printed information and error information into the log file, and start the NodeJs service.
$ pm2 start –l /log/pm2.log –o /log/out.log –e /log/error.log app.js –watch
The above command can detect file changes and then restart the NodeJs service.
Is it easy to use? Of course, the functions of pm2 are far more than that. For details, please refer to the detailed tutorial of pm2 .
The following is an unconventional method. This method is written by myself according to my own development environment. Please correct me if there are any shortcomings.
Automatic restart of NodeJs through SVN
In actual development, I personally use a Linux server. Every time I finish writing NodeJs code locally, I deploy it to the server through Svn. Of course, this requires the use of the SVN hook post-commit. That is, this file is used to restart NodeJs.
If you have any questions about svn synchronizing code to the server, you can refer to the article "Linux server svn remote code synchronization" .
Now we present the shell code directly
#!/bin/bash
export LANG=zh_CN.UTF-8
REPOS="$1"
REV="$2"
SVN=/usr/bin/svn
WEB=/www/nodejs
LOG=/data/home/auto_svn.log
$SVN update $WEB --username uname –password password
#To detect whether the node process exists
ps -A | grep node
#
#The $? is 0 if the node process exists,otherwise 1.
#
#If $? is 0,we will kill the node process.
if [ $? -eq 0 ];then
pid=`ps -A | grep node | awk '{print $1}'`
kill -9 $pid
fi
#Start the node process
nohup node /www/nodejs/index.js >> /log/nodejs.log 2>&1 &
In this way, we can automatically restart the NodeJs service after modifying the code.
The above are the three methods I want to introduce to you. Of course, it needs to be explained here. There are many ways to make NodeJs restart automatically. It is not immutable. We can choose the corresponding method that suits us according to our own development environment.
Just like the first and second methods, they can be said to be universal methods and can be used in different environments. For the third method, if we use SVN to synchronize the code, then there is no problem. If we do not use SVN to synchronize the code, then it is not applicable. Therefore, which method to use depends on your own development environment.
This article will introduce these methods to you, hoping to be helpful to you.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Implementing a group chat room using socket.io
Publish Date:2025/03/18 Views:68 Category:NETWORK
-
This article will share with you an example of using socket.io to realize the function of group chat. If you want to use socket.io, you must use nodejs to implement the server, so we need to install socket.io in nodejs Install socket.io How
NodeJS & HTTP Error Code 431 Request Header Fields
Publish Date:2025/03/17 Views:127 Category:NETWORK
-
I recently discovered that error responses from a Node JS microservice were coming with the HTTP error "431 Request Header Fields Too Large", but at first it seemed to be intermittent, depending on the test environment used. Further investigation reve
使用 NodeJS 检查 MongoDB 中是否存在集合
Publish Date:2023/04/21 Views:216 Category:MongoDB
-
在本文中,我们将检查 MongoDB 数据库中是否存在一个集合,并且我们还将查看与主题相关的示例,以使主题更容易理解。 为此,我们将使用 Node.js。
Node JS 中的 module.exports
Publish Date:2023/03/27 Views:247 Category:Node.js
-
本教程演示了如何在 Node JS 中使用 module.exports。
更新 Node.js
Publish Date:2023/03/12 Views:174 Category:Node.js
-
本教程演示如何在 Windows、macOS 和 Linux 中更新 Node.js。
NodeJS 中的日志记录
Publish Date:2023/03/12 Views:294 Category:Node.js
-
本教程演示如何在 NodeJS 中创建和存储日志,Logging 是记录应用程序流程和行为的过程。Logging 应该在生产模式下执行到更持久的输出流。