如何捕获“响应结束” node.js中的事件+ express? [英] How do I capture a "response end" event in node.js+express?
问题描述
我想在express中执行以下操作:
路由中间件
function(req,res,next){
res.on ('end',function(){
//记录响应代码并处理db
if(res.statusCode< 400){db.commit()} else {db.rollback()}
});
next();
}
路线:
app.post(/ something,function(req,res){
db.doSomething(function(){
if(some problem){
res.send(500);
} else {
res.send(200);
}
});
}
当我尝试这个,结束事件处理程序永远不会被调用,对于 res也是如此。 on('close')
,我在另一篇文章中阅读过,这样的事件是否被解雇?
唯一的其他方式我可以想这样做是在自定义中间件中使用我自己的版本包装 res.end
或 res.send
理想的,因为 res.end
和 res.send
不要采取回调,所以我不能包装他们,打电话给原来的,然后根据当他们打电话给我设置的响应代码(因为他们不会打电话给我)来做我的事情。
是否有一个简单的方式d这个?
阅读文档,这里是 res.send
的签名:
res.send(body | status [,headers | status [,status]]
这意味着您可以设置自己的状态,如下所示: res.send('some string',200);
甚至只是 res.send(404);
。
此方法是您用于发送<
一旦发送到客户端,您将无法访问它,所以没有回调。
这是您的服务器最后一件事。一旦它处理了请求,它会发送响应。
但是,您可以在之前访问,将其发送到客户。这意味着你可以:
console.log(res);
res.send(datas);
如果要回滚/提交,则在调用数据库的回调时执行此操作,而不是在回应消失了。
I'd like to write an express middleware function that sets up a listener on the response's 'end' event, if one exists. The purpose is to do cleanup based on the http response code that the end handler decided to send, e.g. logging the response code and rollback/commit of a db transaction. i.e., I want this cleanup to be transparent to the end caller.
I'd like to do something like the following in express:
The route middleware
function (req, res, next) {
res.on ('end', function () {
// log the response code and handle db
if (res.statusCode < 400) { db.commit() } else { db.rollback() }
});
next();
}
The route:
app.post ("/something", function (req, res) {
db.doSomething (function () {
if (some problem) {
res.send (500);
} else {
res.send (200);
}
});
}
When I try this, the 'end' event handler never gets called. The same for res.on('close')
, which I read about in another post. Do such events get fired?
The only other way I can think of doing this is wrapping res.end
or res.send
with my own version in a custom middleware. This is not ideal, because res.end
and res.send
don't take callbacks, so I can't just wrap them, call the original and then do my thing based on the response code that got set when they call me back (because they won't call me back).
Is there a simple way to do this?
Reading the documentation, here is the signature of res.send
:
res.send(body|status[, headers|status[, status]])
Which means you can set your own status, like this: res.send( 'some string', 200 );
or even just res.send( 404 );
.
This method is the one you use to send the response.
Once it is sent to the client, you can't access it anymore, so there is no callback.
This is the last thing your server does. Once it has processed the request, it sends the response.
However, you can access it before you send it to the client. Which means you can:
console.log( res );
res.send( datas );
If you want to rollback/commit, you do it when the database's callback is called, not when the response is gone.
这篇关于如何捕获“响应结束” node.js中的事件+ express?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!