Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在Express中记录所有HTTP响应_Node.js_Http_Express - Fatal编程技术网

Node.js 在Express中记录所有HTTP响应

Node.js 在Express中记录所有HTTP响应,node.js,http,express,Node.js,Http,Express,在Express中记录所有HTTP响应的最佳方法是什么 我唯一能让它工作的方法就是用猴子修补end事件 app.use(function(req, res, next) { var rEnd = res.end; res.end = function(chunk, encoding, callback) { console.log(chunk.toString('utf8'); rEnd.apply(this, arguments); }; next(); }

在Express中记录所有HTTP响应的最佳方法是什么

我唯一能让它工作的方法就是用猴子修补
end
事件

app.use(function(req, res, next) {
  var rEnd = res.end;

  res.end = function(chunk, encoding, callback) {
    console.log(chunk.toString('utf8');
    rEnd.apply(this, arguments);
  };

  next();
});
我对更优雅的解决方案感兴趣,例如使用
finish
事件,但我无法访问此上下文中的响应消息

app.use(function(req, res, next) {

  res.on('finish', function() {
    // console.log(?);
  });

  next();
});

有很多包记录响应和请求

最常见的是,看看它


大多数软件包只是提供了一个中间件,必须使用
app将其添加到应用程序对象中。请使用
方法。

有很多软件包记录响应和请求

最常见的是,看看它

大多数软件包只是提供了一个中间件,必须使用
app将其添加到你的app对象中。请使用
方法。

我使用过它,它为请求和错误记录提供了一个中间件

import express from 'express'
import winston from 'winston'
import expressWinston from 'express-winston'
import routes from './routes'

const app = express()

// Log the whole request and response body
expressWinston.requestWhitelist.push('body')
expressWinston.responseWhitelist.push('body')

// Logger makes sense before the router
app.use(expressWinston.logger({
  transports: [
    new winston.transports.Console({
      json: true,
      colorize: true
    })
  ]
}))

// Now we can tell the app to use our routing code
app.use(routes);

// Error logger makes sense after the router
app.use(expressWinston.errorLogger({
  transports: [
    new winston.transports.Console({
      json: true,
      colorize: true
    })
  ]
}))

app.listen(3000)
我使用了它,它为请求和错误记录提供了一个中间件

import express from 'express'
import winston from 'winston'
import expressWinston from 'express-winston'
import routes from './routes'

const app = express()

// Log the whole request and response body
expressWinston.requestWhitelist.push('body')
expressWinston.responseWhitelist.push('body')

// Logger makes sense before the router
app.use(expressWinston.logger({
  transports: [
    new winston.transports.Console({
      json: true,
      colorize: true
    })
  ]
}))

// Now we can tell the app to use our routing code
app.use(routes);

// Error logger makes sense after the router
app.use(expressWinston.errorLogger({
  transports: [
    new winston.transports.Console({
      json: true,
      colorize: true
    })
  ]
}))

app.listen(3000)

使用“Morgan”和“Rotating File Stream”,您可以记录事务(请求和响应)并将其存储在一段时间间隔内。

var express= require('express');
var router = express.Router();
//we get an instance of express  
var app = express();
var rfs = require('rotating-file-stream');
var morgan = require('morgan');
//log file rotation
var logDirectory = path.join(__dirname, 'log')
// ensure log directory exists 
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)
// create a rotating write stream 
var accessLogStream = rfs('access.log', {
  interval: '1d', // rotate daily 
  path: logDirectory
});
app.use(morgan('combined', {stream: accessLogStream}));

使用“Morgan”和“Rotating File Stream”,您可以记录事务(请求和响应)并将其存储在一段时间间隔内。

var express= require('express');
var router = express.Router();
//we get an instance of express  
var app = express();
var rfs = require('rotating-file-stream');
var morgan = require('morgan');
//log file rotation
var logDirectory = path.join(__dirname, 'log')
// ensure log directory exists 
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)
// create a rotating write stream 
var accessLogStream = rfs('access.log', {
  interval: '1d', // rotate daily 
  path: logDirectory
});
app.use(morgan('combined', {stream: accessLogStream}));

无法使用morganIt记录HTTP参数无法使用Morganin在
finish
事件函数中记录HTTP参数您可以,例如,使用
res.getHeaders()
res.statusCode
获取
finish
事件函数中的响应数据您可以使用
res.getHeaders()
res.statusCode
获取响应数据