我正在为模块编写测试脚本,完成请求后,我需要能够关闭服务器.以下代码可以正常工作,但我可以看到app.close()已从express 3中删除.现在最好的方法是什么?
I'm writing a test script for my module and I need to be able to close my server when my request is done. The following code works but I can see that app.close() was dropped from express 3. What's the best way to do it now?
var testCase = require('nodeunit').testCase; var request = require('request'); var express = require('express'); var app = express.createServer(); var srv = app.listen(); .... request({ method: 'POST', json: true, body: { id: 'second request'}, url: '' + target }, function(err, res, body) { console.info("closing server"); app.close(); test.done(); }); });谢谢, 李
p.s.关闭服务器后必须调用test.done(),否则测试将失败.
p.s. test.done() must be called after closing the server, otherwise the test will fail.
推荐答案用于从http.Server继承的Express应用程序,它们不再这样做,而这正是close方法的来源.而是在您的srv实例上调用close().您可能通常会看到此代码写为:
Express applications used to inherit from http.Server, which they no longer do, and that's where the close method came from. Instead, call close() on your srv instance. You may commonly see this code written as:
var app = express.createServer(); var srv = require('http').createServer(app); srv.listen(port);根据app.listen()的文档:
绑定并侦听给定主机和端口上的连接,此方法与节点的http.Server#listen()相同.
更多推荐
请求完成后,nodejs表示关闭服务器
发布评论