node.js 如何以编程方式关闭 ExpressJS 实例进行测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8659011/
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 do I programmatically shut down an instance of ExpressJS for testing?
提问by drewww
I'm trying to figure out how to shut down an instance of Express. Basically, I want the inverse of the .listen(port)call - how do I get an Express server to STOP listening, release the port, and shutdown cleanly?
我想弄清楚如何关闭 Express 的一个实例。基本上,我想要相反的.listen(port)调用 - 如何让 Express 服务器停止侦听、释放端口并彻底关闭?
I know this seems like it might be a strange query, so here's the context; maybe there's another way to approach this and I'm thinking about it the wrong way. I'm trying to setup a testing framework for my socket.io/nodejs app. It's a single-page app, so in my testing scripts (I'm using Mocha, but that doesn't really matter) I want to be able to start up the server, run tests against it, and then shut the server down. I can get around this by assuming that either the server is turned on before the test starts or by having one of the tests start the server and having every subsequent test assume it's up, but that's really messy. I would much prefer to have each test file start a server instance with the appropriate settings and then shut that instance down when the tests are over. That means there's no weird dependencies to running the test and everything is clean. It also means I can do startup/shutdown testing.
我知道这似乎是一个奇怪的查询,所以这是上下文;也许有另一种方法可以解决这个问题,而我正在以错误的方式思考它。我正在尝试为我的 socket.io/nodejs 应用程序设置一个测试框架。这是一个单页应用程序,所以在我的测试脚本中(我使用的是Mocha,但这并不重要)我希望能够启动服务器,对其运行测试,然后关闭服务器。我可以通过假设服务器在测试开始之前打开或者让其中一个测试启动服务器并让每个后续测试假设它已经启动来解决这个问题,但这真的很混乱。我更希望让每个测试文件使用适当的设置启动一个服务器实例,然后在测试结束时关闭该实例。这意味着运行测试没有奇怪的依赖关系,一切都很干净。这也意味着我可以进行启动/关闭测试。
So, any advice about how to do this? I've thought about manually triggering exceptions to bring it down, but that seems messy. I've dug through Express docs and source, but can't seem to find any method that will shut down the server. There might also be something in socket.io for this, but since the socket server is just attached to the Express server, I think this needs to happen at the express layer.
那么,关于如何做到这一点有什么建议吗?我曾考虑过手动触发异常以将其关闭,但这似乎很混乱。我已经挖掘了 Express 文档和源代码,但似乎找不到任何可以关闭服务器的方法。socket.io 中可能也有一些东西,但由于套接字服务器只是附加到 Express 服务器,我认为这需要在 express 层发生。
回答by Rich Apodaca
Things have changedbecause the express server no longer inherits from the node http server. Fortunately, app.listen returns the server instance.
事情发生了变化,因为 express 服务器不再从节点 http 服务器继承。幸运的是, app.listen 返回服务器实例。
var server = app.listen(3000);
// listen for an event
var handler = function() {
server.close();
};
回答by Srijan Choudhary
Use app.close(). Full example:
使用app.close(). 完整示例:
var app = require('express').createServer();
app.get('/', function(req, res){
res.send('hello world');
});
app.get('/quit', function(req,res) {
res.send('closing..');
app.close();
});
app.listen(3000);
Call app.close()inside the callback when tests have ended. But remember that the process is still running(though it is not listening anymore).
调用app.close()回调函数内部时,测试已经结束。但请记住,该进程仍在运行(尽管它不再侦听)。
If after this, you need to end the process, then call process.exit(0).
如果在此之后,您需要结束该过程,则调用process.exit(0)。
Links:
链接:
app.close: http://nodejs.org/docs/latest/api/http.html#server.close(same applies for)
app.close:http://nodejs.org/docs/latest/api/http.html#server.close (同样适用)
process.exit: http://nodejs.org/docs/latest/api/process.html#process.exit
process.exit:http: //nodejs.org/docs/latest/api/process.html#process.exit
回答by Gajus
I have answered a variation of "how to terminate a HTTP server" many times on different node.jssupport channels. Unfortunately, I couldn't recommend any of the existing libraries because they are lackingin one or another way. I have since put together a package that (I believe) is handling all the cases expected of graceful HTTP server termination.
我已经在不同的node.js支持频道上多次回答了“如何终止 HTTP 服务器”的变体。不幸的是,我不能推荐任何现有的库,因为它们以一种或另一种方式缺乏。从那以后,我整理了一个包(我相信)可以处理所有期望 HTTP 服务器正常终止的情况。
https://github.com/gajus/http-terminator
https://github.com/gajus/http-terminator
The main benefit of http-terminatoris that:
http-terminator的主要好处是:
- it does not monkey-patch Node.js API
- it immediately destroys all sockets without an attached HTTP request
- it allows graceful timeout to sockets with ongoing HTTP requests
- it properly handles HTTPS connections
- it informs connections using keep-alive that server is shutting down by setting a connection: close header
- it does not terminate the Node.js process
- 它不会修补 Node.js API
- 它立即销毁所有没有附加 HTTP 请求的套接字
- 它允许对正在进行的 HTTP 请求的套接字进行正常超时
- 它正确处理 HTTPS 连接
- 它通过设置连接通知使用 keep-alive 服务器正在关闭的连接:关闭标头
- 它不会终止 Node.js 进程
Usage with Express.js:
与 Express.js 一起使用:
import express from 'express';
import {
createHttpTerminator,
} from 'http-terminator';
const app = express();
const server = app.listen();
const httpTerminator = createHttpTerminator({
server,
});
await httpTerminator.terminate();
回答by аlex dyky?
//... some stuff
var server = app.listen(3000);
server.close();
回答by Josh Smith
You can easily do this by writing a bash script to start the server, run the tests, and stop the server. This has the advantage of allowing you to alias to that script to run all your tests quickly and easily.
您可以通过编写 bash 脚本来启动服务器、运行测试和停止服务器来轻松完成此操作。这具有允许您为该脚本添加别名以快速轻松地运行所有测试的优点。
I use such scripts for my entire continuous deployment process. You should look at Jon Rohan's Dead Simple Git Workflowfor some insight on this.
我在整个持续部署过程中使用此类脚本。您应该查看Jon Rohan 的 Dead Simple Git Workflow以获得有关此的一些见解。

