Nodejs:在 res.send 之后做额外的事情
Nodejs: Do additional stuff after res.send
我将 Node 用作网络服务器,我想将对它的每个请求都记录到数据库中。我也希望用户尽快收到响应,所以我想出了这个代码:
// ... putting together the response_data
res.send(response_data);
// ... now log the request into the DB and maybe do additional stuff
它有效,我喜欢将一些(时间)昂贵的东西放在发送后面的想法。但是由于我是 Node 的新手,我想问这是否是一种常见模式?
在 Whosebug 上,我发现人们有问题,因为他们试图在 res.send 之后发送额外的数据 - 但我从未听到有人说 "yeah this is a great feature for your responsiveness",所以我不确定是否存在重大缺陷这个解决方案我还没有看到...
只要您不需要因 "additional" 内容而将任何内容发送回用户,那么您的方法就可以了。
大多数人遇到的问题是试图在响应已经发送后发送数据,例如
res.send(response_data);
// do additional stuff
res.send(additional_data); // KABOOM!
我将 Node 用作网络服务器,我想将对它的每个请求都记录到数据库中。我也希望用户尽快收到响应,所以我想出了这个代码:
// ... putting together the response_data
res.send(response_data);
// ... now log the request into the DB and maybe do additional stuff
它有效,我喜欢将一些(时间)昂贵的东西放在发送后面的想法。但是由于我是 Node 的新手,我想问这是否是一种常见模式?
在 Whosebug 上,我发现人们有问题,因为他们试图在 res.send 之后发送额外的数据 - 但我从未听到有人说 "yeah this is a great feature for your responsiveness",所以我不确定是否存在重大缺陷这个解决方案我还没有看到...
只要您不需要因 "additional" 内容而将任何内容发送回用户,那么您的方法就可以了。
大多数人遇到的问题是试图在响应已经发送后发送数据,例如
res.send(response_data);
// do additional stuff
res.send(additional_data); // KABOOM!