In a Express application I would like to do something like this:
在Express应用程序中,我想做这样的事情:
app.get('/some/route', someMiddleWare(), function(req, res){
var status = undefined;
if( /*someCondition*/ ) status = 200;
else status = 403
res.status(status).send('');
// do something else
})
In the first part I do something that is needed in order to decode what response to give, in the second (after send()
) I do something additional that needs to be done in the same execution (i.e. not asynchronously) but does not really concern the user. The question is: can I be sure that after send()
returns the response is already on its way back to the user? Or is it sent only after the execution of my handler function? Thanks
在第一部分中,我做了一些事情,以便解码要给出的响应,在第二部分(在send()之后)我做了一些额外的事情,需要在同一个执行中完成(即不是异步)但是并不是真的关心用户。问题是:我可以确定send()返回后响应已经回到用户的路上吗?或者它是在执行我的处理函数后才发送的?谢谢
2 个解决方案
#1
0
The question is: can I be sure that after
send()
returns the response is already on its way back to the user? Or is it sent only after the execution of my handler function?问题是:我可以确定send()返回后响应已经回到用户的路上吗?或者它是在执行我的处理函数后才发送的?
Yes, you can be sure the data is on its way. At the point you call res.send()
, the data has been written to the underlying TCP infrastructure and it is then up to the TCP stack to send it. Due to TCP Nagle algorithm, there might be a short delay before the data is sent, but it will not be waiting for anything in your Express application. The data will be sent as soon as the TCP stack decides to send it.
是的,您可以确定数据正在进行中。在调用res.send()时,数据已写入底层TCP基础结构,然后由TCP堆栈发送。由于TCP Nagle算法,在发送数据之前可能会有短暂的延迟,但它不会等待Express应用程序中的任何内容。一旦TCP堆栈决定发送数据,就会发送数据。
It won't matter whether you are doing something else in the request handler after you send the response or not.
在发送响应之后,您是否在请求处理程序中执行其他操作无关紧要。
If you want to follow the actual source code for this, you first look at res.send(data)
in express and that ultimately ends up calling res.end(data)
in the http module. And, by looking at the http module, you can see that it is writing the data to the TCP implementation synchronously when it is called. There are no magic delays waiting for other things to happen in Express.
如果你想遵循这个实际的源代码,你首先要看快递中的res.send(data),最终最终在http模块中调用res.end(data)。并且,通过查看http模块,您可以看到它在调用时同步地将数据写入TCP实现。在Express中等待其他事情没有神奇的延迟。
#2
1
Yes. You're code will execute in the function even after you send the data through res.send()
.
是。即使通过res.send()发送数据,您的代码也会在函数中执行。
However, after you use res.send()
you will no longer be able to send any other data with that same response to that request.
但是,在使用res.send()之后,您将无法再向该请求发送具有相同响应的任何其他数据。
#1
0
The question is: can I be sure that after
send()
returns the response is already on its way back to the user? Or is it sent only after the execution of my handler function?问题是:我可以确定send()返回后响应已经回到用户的路上吗?或者它是在执行我的处理函数后才发送的?
Yes, you can be sure the data is on its way. At the point you call res.send()
, the data has been written to the underlying TCP infrastructure and it is then up to the TCP stack to send it. Due to TCP Nagle algorithm, there might be a short delay before the data is sent, but it will not be waiting for anything in your Express application. The data will be sent as soon as the TCP stack decides to send it.
是的,您可以确定数据正在进行中。在调用res.send()时,数据已写入底层TCP基础结构,然后由TCP堆栈发送。由于TCP Nagle算法,在发送数据之前可能会有短暂的延迟,但它不会等待Express应用程序中的任何内容。一旦TCP堆栈决定发送数据,就会发送数据。
It won't matter whether you are doing something else in the request handler after you send the response or not.
在发送响应之后,您是否在请求处理程序中执行其他操作无关紧要。
If you want to follow the actual source code for this, you first look at res.send(data)
in express and that ultimately ends up calling res.end(data)
in the http module. And, by looking at the http module, you can see that it is writing the data to the TCP implementation synchronously when it is called. There are no magic delays waiting for other things to happen in Express.
如果你想遵循这个实际的源代码,你首先要看快递中的res.send(data),最终最终在http模块中调用res.end(data)。并且,通过查看http模块,您可以看到它在调用时同步地将数据写入TCP实现。在Express中等待其他事情没有神奇的延迟。
#2
1
Yes. You're code will execute in the function even after you send the data through res.send()
.
是。即使通过res.send()发送数据,您的代码也会在函数中执行。
However, after you use res.send()
you will no longer be able to send any other data with that same response to that request.
但是,在使用res.send()之后,您将无法再向该请求发送具有相同响应的任何其他数据。