简体   繁体   English

节点表示服务器request.on('end')仅触发获取

[英]node express server request.on('end') only firing for get

I am trying to create a middleware function that fires when the request has ended. 我正在尝试创建一个中间件函数,该函数在请求结束时触发。

middleware 中间件

module.exports = function(req, res, next) {
    req.on("end", function() {
        console.log('End');
    });
    next();
};

I include my middleware like this. 我包括这样的中间件。

app.all('/*', [require('./middlewares/activityLogger.js')]);

at the moment i see "END in console for GET request but not for POST, PUT and DELETE" 目前,我看到“控制台中的GET请求结束,而POST,PUT和DELETE则没有”

example of how i end request GET (WORKS) 我如何结束GET(WORKS)的示例

res.status(200).json(result)

POST (Don't work) 开机自检(无效)

res.status(201).json(result.create)

any suggesting to why my middleware function is only fired on GET requests? 有什么建议为什么我的中间件功能仅在GET请求上触发?

Solved it by listening to the "finish" event instead of "end" 通过听“完成”事件而不是“结束”来解决它

module.exports = function(req, res, next) {
    res.on('finish', function() {
        console.log('finish');
    });
    next();
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM