简体   繁体   English

如何重新启动 node.js 服务器

[英]How to restart a node.js server

I've installed and is running a node.js server on osx.我已经在 osx 上安装并运行 node.js 服务器。 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.我已经更改了一些部分,需要重新启动服务器才能看到效果。

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.谢谢。

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 PID is replaced by the number in the output of ps .其中PIDps输出中的数字替换。

During development the best way to restart server for seeing changes made is to use nodemon在开发过程中,重启服务器以查看所做更改的最佳方法是使用nodemon

npm install nodemon -g npm install nodemon -g

nodemon [your app name] 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 nodemon git repo: https://github.com/remy/nodemon检查nodemon git repo: https : //github.com/remy/nodemon

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.在这种情况下,您经常重新启动 node.js 服务器,因为它正在积极开发中,并且您一直在进行更改。 There is a great hot reload script 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.有一个很棒的热重载脚本,它会通过观察所有 .js 文件并在其中任何文件发生更改时重新启动 node.js 服务器来为您处理此问题。 Just the ticket for rapid development and test.只是快速开发和测试的门票。

The script and explanation on how to use it are at here at Draco Blue .有关如何使用它的脚本和说明位于Draco Blue此处。

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 自己的进程,所以我想在某些情况下它也可能会杀死一些具有类似名称的其他进程)

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

I understand that my comment relate with windows, but may be someone be useful.我知道我的评论与 Windows 相关,但可能有人有用。 For win run in cmd:对于在 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

在“kill -2 [PID]”不起作用的情况下,使用“kill -9 [PID]”或“killall -9 node”对我有用。

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();
});

At first open terminal/command line then go to your project directory, now install nodemon by using command npm install nodemon --save-dev this command will make sure it saved as developer dependency.在第一次打开终端/命令行,然后去你的项目目录,现在通过使用命令NPM安装nodemon --save-dev的这个命令将确保它保存为开发者依赖安装nodemon。 If you are working with expressjs then in your package file it will look like如果你正在使用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.现在修改 package.json 文件中的“start”值,对于生产,我们将使用现有值,但对于开发,将使用 nodemon 来跟踪源文件中的更改,而无需重新启动服务器。 There for new value for start is "start": "if [[$NODE_ENV=='production']]; then node ./bin/www; else nodemon ./bin/www; fi" 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM