如何在不终止进程的情况下停止 node.js 服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8943136/
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 stop a node.js server without killing the process?
提问by Fluffy
Say a server is created like:
假设一个服务器是这样创建的:
var net = require('net');
var server = net.createServer();
server.listen(32323, '127.0.0.1');
server.close(); // gives a "not running" error if there is IP in the listen statement
is there a way to stop it from node.js, e.g. without ending/killing the whole process?
有没有办法从 node.js 中阻止它,例如不结束/终止整个过程?
Also, here's a more complicated example that doesn't stop regardless of the IP the server is bound to:
此外,这是一个更复杂的示例,无论服务器绑定到哪个 IP,它都不会停止:
var net = require('net');
var server = net.createServer(function(socket) {
socket.on('data', console.log);
server.close();
});
server.listen(32323);
var socket = net.createConnection(32323);
socket.write('hi');
采纳答案by Raynos
Do not call close before the "listening"event fires.
在"listening"事件触发之前不要调用 close 。
Either add a callback to listenor add an event manually
listen手动添加回调或添加事件
server.listen(port, host, function () { server.close(); });
// OR
server.on("listening", function () { server.close(); });
server.listen(port, host);
var net = require('net');
var server = net.createServer(function(socket) {
socket.on('data', console.log);
server.close();
});
server.listen(32323);
var socket = net.createConnection(32323);
// call end to make sure the socket closes
socket.end('hi');
回答by Ricardo Tomasi
Skipping to the 2nd, more realistic case: close the current connection before calling server.close:
跳到第二个更现实的情况:在调用之前关闭当前连接server.close:
var net = require('net')
var server = net.createServer(function(socket){
socket.on('data', function(data){
console.log(data.toString())
socket.destroy()
server.close()
})
})
server.listen(32323)
Note that if you don't have any other server instances or activity going on in this process it will quit after closing server.
请注意,如果您在此过程中没有任何其他服务器实例或活动,它将在关闭后退出server。
回答by fwolle30
If it's a HTTP or HTTPS server, it helps if you close the socket of the request with the following:
如果它是 HTTP 或 HTTPS 服务器,则使用以下命令关闭请求的套接字会有所帮助:
function onRequest(request, response) {
response.end(); //close the response
request.connection.end(); //close the socket
request.connection.destroy; //close it really
server.close(); //close the server
}
server.on("request", onRequest);
server.on("close", function() {console.log("closed");});
server.listen(port, host);
Hope that helps.
希望有帮助。
回答by kberg
Here is an example demonstrating what I think you're after.
这是一个示例,展示了我认为您所追求的内容。
var net = require('net');
var server = net.createServer(function (socket) {
socket.on('data', function (data) {
console.log("server got data " + data.toString());
});
});
function startServer() {
server.listen(32323, function () {
console.log('server started');
});
}
function stopServer() {
server.close(function () {
console.log('server stopped');
});
}
setInterval(function () {
if (server.address()) {
console.log("Server is running on port " + server.address().port + " let's kill it.");
stopServer();
} else {
console.log("Server is stopped let's start it.");
startServer();
}
}, 10000);
setInterval(function () {
var socket = net.createConnection(32323, function () {
socket.write('hi');
});
socket.on('error', function (e) {
console.log(e.message);
});
}, 1000);
startServer();
回答by teknopaul
server.close()prevents new connections and waits until all the clients are closed.
server.close()阻止新连接并等待所有客户端都关闭。
To forcibly kill a node server you need to call server.close() and then close all the open connections from the server end.
要强行杀死节点服务器,您需要调用 server.close() ,然后关闭服务器端所有打开的连接。
When a client connects keep track of the socket.
当客户端连接时,跟踪套接字。
var sockets = []
var server = net.createServer((socket) => {
sockets.push(socket)
})
To forcibly kill ...
强行杀...
server.close()
sockets.forEach( (s) => s.end() )

