如何在Express中捕获(或记录)Response对象的数据?

时间:2016-09-16 18:28:35

标签: node.js express logging

我需要捕获已发送的数据。我知道如何设置中间件并捕获请求和响应头。

我要求捕获发送的数据。

我正在尝试这个:

var express = require('express')
var app = express();

var onFinished = require('on-finished')

app.listen(3000);

function logRes(res){
    console.log('* captured response *')
    console.log(res._header)
    console.log(res.statusCode)
    console.log(res.statusMessage)
    console.log(res.body)  // THIS DOES NOT WORKS!
}

app.use(function(req,res,next){
    onFinished(res, function (err, res) {
        logRes(res)
    })
    console.log('= captured request =');
    console.log(req.method)
    console.log(req.path)
    console.log(req.query)
    next();
});

app.get('/example',function(req,res){
    res.header('Content-Type','text/plain');
    res.end('example data');
});

有人能告诉我如何使用// THIS DOES NOT WORKS!评论来查看已发送数据吗?

1 个答案:

答案 0 :(得分:-3)

首先需要包含第三方中间件body{ margin: 0 !important; } nav{ height: 60px; width: 100%; display: flex; justify-content: center; align-items: center; background-color: rgb(21, 138, 194); } nav > a{ margin: 0 1em; display: flex; color: #036088; text-transform: capitalize; text-decoration: none; } nav > a:hover{ color: white; }

body-parser

检查回购和文档here