如何重新启动 node.js 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3302959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to restart a node.js server
提问by jsnoob
I've installed and is running a node.js server on osx. I've dled a chat module and is happily running it. I've altered some pieces and need to restart the server to see the effects.
我已经在 osx 上安装并运行 node.js 服务器。我已经开发了一个聊天模块并且很高兴地运行它。我已经更改了一些部分,需要重新启动服务器才能看到效果。
I only know how to restart by closing the terminal window and then reopneing it and then running node chatdemo.js again.
我只知道如何通过关闭终端窗口然后重新打开它然后再次运行 node chatdemo.js 来重新启动。
Any way to restart without closing terminal?
有什么方法可以在不关闭终端的情况下重新启动?
Thanks.
谢谢。
回答by rfunduk
If it's just running (not a daemon) then just use Ctrl-C.
如果它只是在运行(不是守护进程),那么只需使用Ctrl-C.
If it's daemonized then you could try:
如果它是守护进程,那么你可以尝试:
$ ps aux | grep node
you PID 1.5 0.2 44172 8260 pts/2 S 15:25 0:00 node app.js
$ kill -2 PID
Where PIDis replaced by the number in the output of ps.
wherePID由 的输出中的数字代替ps。
回答by Nikhil Vishnu
During development the best way to restart server for seeing changes made is to use nodemon
在开发过程中,重启服务器以查看所做更改的最佳方法是使用 nodemon
npm install nodemon -g
nodemon [your app name]
npm install nodemon -g
nodemon [您的应用程序名称]
nodemon will watch the files in the directory that nodemon was started, and if they change, it will automatically restart your node application.
nodemon 将监视 nodemon 启动目录中的文件,如果它们发生变化,它将自动重新启动您的 node 应用程序。
Check nodemongit repo: https://github.com/remy/nodemon
检查nodemongit repo:https: //github.com/remy/nodemon
回答by Paul Robinson
In this case you are restarting your node.js server often because it's in active development and you are making changes all the time. There is a great hot reloadscript that will handle this for you by watching all your .js files and restarting your node.js server if any of those files have changed. Just the ticket for rapid development and test.
在这种情况下,您经常重新启动 node.js 服务器,因为它正在积极开发中,并且您一直在进行更改。有一个很棒的热重载脚本,它会通过查看所有 .js 文件并在其中任何文件发生更改时重新启动 node.js 服务器来为您处理此问题。只是快速开发和测试的门票。
The script and explanation on how to use it are at here at Draco Blue.
有关如何使用它的脚本和说明位于Draco Blue此处。
回答by Crocodile
I had the same problem and then wrote this shell script which kills all of the existing node processes:
我遇到了同样的问题,然后编写了这个 shell 脚本来杀死所有现有的节点进程:
#!/bin/bash
echo "The following node processes were found:"
ps aux | grep " node " | grep -v grep
nodepids=$(ps aux | grep " node " | grep -v grep | cut -c10-15)
echo "OK, so we will stop these process/es now..."
for nodepid in ${nodepids[@]}
do
echo "Stopping PID :"$nodepid
kill -9 $nodepid
done
echo "Done"
After this is saved as a shell script (xxx.sh) file you might want to add it to your PATH as described here.
在此之后被保存为一个shell脚本(xxx.sh)文件你可能想描述将它添加到您的PATH这里。
(Please note that this will kill all of the processes with " node " in it's name except grep's own, so I guess in some cases it may also kill some other processes with a similar name)
(请注意,这将杀死所有名称中带有“ node ”的进程,除了 grep 自己的进程,所以我想在某些情况下它也可能会杀死一些具有类似名称的其他进程)
回答by dimaqw
I understand that my comment relate with windows, but may be someone be useful. For win run in cmd:
我知道我的评论与 Windows 相关,但可能有人有用。对于在 cmd 中运行的 win:
wmic process where "commandline like '%my_app.js%' AND name='node.exe' " CALL Terminate
then you can run your app again:
然后你可以再次运行你的应用程序:
node my_app.js
Also you can use it in batch file, with escape quotes:
您也可以在批处理文件中使用它,并带有转义引号:
wmic process where "commandline like '%%my_app.js%%' AND name='node.exe' " CALL Terminate
node my_app.js
回答by spaxxUnited
To say "nodemon" would answer the question.
说“nodemon”会回答这个问题。
But on how only to kill (all) node demon(s), the following works for me:
但是关于如何只杀死(所有)节点恶魔,以下对我有用:
pkill -HUP node
回答by Steven H
Using "kill -9 [PID]" or "killall -9 node" worked for me where "kill -2 [PID]" did not work.
在“kill -2 [PID]”不起作用的情况下,使用“kill -9 [PID]”或“killall -9 node”对我有用。
回答by user511941
If I am just run the node app from console (not using forever etc) I use control + C, not sure if OSX has the same key combination to terminate but much faster than finding the process id and killing it, you could also add the following code to the chat app you are using and then type 'exit' in the console whenever you want to close down the app.
如果我只是从控制台运行节点应用程序(不使用永远等),我使用 control + C,不确定 OSX 是否具有相同的组合键来终止但比找到进程 ID 并杀死它快得多,您还可以添加将以下代码添加到您正在使用的聊天应用程序中,然后在您想关闭应用程序时在控制台中键入“退出”。
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data) {
if (data == 'exit\n') process.exit();
});
回答by Lord
At first open terminal/command line then go to your project directory, now install nodemonby using command npm install nodemon --save-devthis command will make sure it saved as developer dependency. If you are working with expressjsthen in your package file it will look like
在第一次打开终端/命令行,然后去你的项目目录,现在安装nodemon使用命令NPM安装nodemon --save-dev的这个命令将确保它保存为开发人员的依赖。如果你正在使用expressjs那么在你的包文件中它看起来像
{
"name": "expressjs-app",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"morgan": "~1.9.1",
"pug": "^2.0.4"
},
"devDependencies": {
"nodemon": "^2.0.3"
}
}
now modify the "start" value in your package.json file, for production we will use the exsiting value but for development will use nodemon to track the changes in source file without restarting server. There for new value for start is "start": "if [[$NODE_ENV=='production']]; then node ./bin/www; else nodemon ./bin/www; fi"
现在修改 package.json 文件中的“start”值,对于生产,我们将使用现有值,但对于开发,将使用 nodemon 来跟踪源文件中的更改,而无需重新启动服务器。start 的新值是 "start": "if [[$NODE_ENV=='production']]; then node ./bin/www; else nodemon ./bin/www; fi"
final package.json file will look like
最终的 package.json 文件看起来像
{
"name": "expressjs-app",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "if [[$NODE_ENV=='production']]; then node ./bin/www; else nodemon ./bin/www; fi"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"morgan": "~1.9.1",
"pug": "^2.0.4"
},
"devDependencies": {
"nodemon": "^2.0.3"
}
}
to uninstall nodemon jusy simply run the command npm uninstall nodemon
卸载 nodemon jusy 只需运行命令npm uninstall nodemon

