从命令行停止 node.js 程序 [英] Stop node.js program from command line

查看:59
本文介绍了从命令行停止 node.js 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个侦听端口的简单 TCP 服务器.

I have a simple TCP server that listens on a port.

var net = require("net");

var server = net.createServer(function(socket) {
    socket.end("Hello!
");
});

server.listen(7777);

我用 node server.js 启动它,然后在 Mac 上用 Ctrl + Z 关闭它.当我尝试使用 node server.js 再次运行它时,我收到以下错误消息:

I start it with node server.js and then close it with Ctrl + Z on Mac. When I try to run it again with node server.js I get this error message:

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
          ^
Error: listen EADDRINUSE
at errnoException (net.js:670:11)
at Array.0 (net.js:771:26)
at EventEmitter._tickCallback (node.js:192:41)

我是否以错误的方式关闭程序?我怎样才能防止这种情况发生?

Am I closing the program the wrong way? How can I prevent this from happening?

推荐答案

要结束程序,你应该使用 Ctrl + C.如果你这样做了,它会发送 SIGINT,它允许程序优雅地结束,从它正在侦听的任何端口中解除绑定.

To end the program, you should be using Ctrl + C. If you do that, it sends SIGINT, which allows the program to end gracefully, unbinding from any ports it is listening on.

另请参阅:https://superuser.com/a/262948/48624

这篇关于从命令行停止 node.js 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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