优雅地退出node.js [英] Quitting node.js gracefully

查看:85
本文介绍了优雅地退出node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读优秀的在线图书 http://nodebeginner.org/,并尝试简单的代码

I'm reading through the excellent online book http://nodebeginner.org/ and trying out the simple code

var http = require("http");

function onRequest(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}

http.createServer(onRequest).listen(8888); 

现在我不知道(而且我仍然不知道!)如何优雅地关闭node.js,所以我就去了ctrl+z.现在,每次我尝试运行node server.js时,我都会收到以下错误消息.

Now I didn't know (and I still don't know!) how to shut down node.js gracefully, so I just went ctrl+z. Now each time I try to run node server.js I get the following error messages.

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: EADDRINUSE, Address already in use
    at Server._doListen (net.js:1100:5)
    at net.js:1071:14
    at Object.lookup (dns.js:153:45)
    at Server.listen (net.js:1065:20)
    at Object.<anonymous> (/Users/Bob/server.js:7:4)
    at Module._compile (module.js:402:26)
    at Object..js (module.js:408:10)
    at Module.load (module.js:334:31)
    at Function._load (module.js:293:12)
    at Array.<anonymous> (module.js:421:10)

因此,有两个问题:

1)如何正常关闭node.js?

1) How do I shut down node.js gracefully?

2)如何修复我创建的烂摊子?

2) How do I repair the mess I've created?

推荐答案

  1. 使用 Ctrl + C 正常退出节点进程

  1. Use Ctrl+C to exit the node process gracefully

要清理混乱情况取决于您的平台,但是基本上您需要找到运行节点的进程的剩余部分并杀死它.

To clean up the mess depends on your platform, but basically you need to find the remains of the process in which node was running and kill it.

例如,在Unix上:ps -ax | grep node将为您提供以下条目:

For example, on Unix: ps -ax | grep node will give you an entry like:

1039 ttys000    0:00.11 node index.js

其中index.js是您的节点文件的名称.

where index.js is the name of your node file.

在此示例中,1039是进程ID(您将有所不同),因此kill -9 1039将结束它,您将能够再次绑定到端口.

In this example, 1039 is the process id (yours will be different), so kill -9 1039 will end it, and you'll be able to bind to the port again.

这篇关于优雅地退出node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆