node.js 为什么我可以在“res.send”之后执行代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16180502/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:17:51  来源:igfitidea点击:

Why can I execute code after "res.send"?

node.jsexpress

提问by Leonidas

I'm wondering what the mechanics behind the behaviour of the following code are:

我想知道以下代码行为背后的机制是什么:

res.send(200, { data: 'test data' });
console.log('still here...');

My understanding is that res.senddoesn't returnthe function, but does close the connection / end the request. This could explain why I can still execute code after a res.sendcommand (I looked through the express source and it doesn't seem to be an asynchronous function).

我的理解是res.send不会返回函数,但会关闭连接/结束请求。这可以解释为什么我仍然可以在res.send命令后执行代码(我查看了 express 源代码,它似乎不是一个异步函数)。

Is there something else at play that I may be missing?

还有什么我可能会遗漏的东西吗?

回答by josh3736

Sure endends the HTTP response, but it doesn't do anything special to your code.

当然会end结束 HTTP 响应,但它不会对您的代码做任何特殊处理。

You can continue doing other things even after you've ended a response.

即使在结束回复后,您也可以继续做其他事情。

What you can'tdo, however, is do anything useful with res. Since the response is over, you can't write more data to it.

但是,您不能做的是对res. 由于响应结束,您无法向其写入更多数据。

res.send(...);
res.write('more stuff'); // throws an error since res is now closed

This behavior is unlike other traditional frameworks (PHP, ASP, etc) which allocate a thread to a HTTP request and terminate the thread when the response is over. If you call an equivalent function like ASP's Response.End, the thread terminates and your code stops running. In node, there is no thread to stop. reqand reswon't fire any more events, but the code in your callbacks is free to continue running (so long as it does not attempt to call methods on resthat require a valid response to be open).

这种行为与其他传统框架(PHP、ASP 等)不同,后者为 HTTP 请求分配一个线程并在响应结束时终止该线程。如果您调用 ASP 之类的等效函数Response.End,线程将终止并且您的代码停止运行。在节点中,没有要停止的线程。 req并且res不会再触发任何事件,但是回调中的代码可以自由地继续运行(只要它不尝试调用res需要打开有效响应的方法)。

回答by Marcos Pereira

Edit:I no longer do what is explained below, as you shouldn't return a value when there is no need for it. It makes your code less readable and looks hackish. Instead, I suggest separating the return statement from the res.send(). @slavafomin explained this well in the comments.

编辑:我不再做下面解释的事情,因为当不需要它时你不应该返回一个值。它使您的代码可读性降低并且看起来很黑。相反,我建议将 return 语句与res.send(). @slavafomin 在评论中很好地解释了这一点。

A simple way to stop the execution of the function and send a response at the same time is by doing

停止函数执行并同时发送响应的一种简单方法是执行

return res.send('500', 'Error message here');

This allows you to use short ifstatements to handle errors such as:

这允许您使用简短的if语句来处理错误,例如:

if (err) {
    return res.send('500', 'Error message here');
}

The exact returnof the res.sendfunction is an objectthat seems to contain the entire state of the connection after you ended it(request, status, headers, etc.), but this should be unimportant since you won't be doing anything with it.

确切的返回的的res.send函数是一个对象似乎包含了连接的整个状态,你结束之后(请求,状态,标题等),但是这应该是不重要的,因为你不会做任何事的。