请求完成后,nodejs表示关闭服务器

编程入门 行业动态 更新时间:2024-10-18 19:29:30
本文介绍了请求完成后,nodejs表示关闭服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在为模块编写测试脚本,完成请求后,我需要能够关闭服务器.以下代码可以正常工作,但我可以看到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表示关闭服务器

本文发布于:2023-11-14 09:50:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586894.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:服务器   完成后   nodejs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!