Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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 有没有一种方法可以获取微服务应用程序http请求的请求和响应正文?_Node.js_Microservices_Trace - Fatal编程技术网

Node.js 有没有一种方法可以获取微服务应用程序http请求的请求和响应正文?

Node.js 有没有一种方法可以获取微服务应用程序http请求的请求和响应正文?,node.js,microservices,trace,Node.js,Microservices,Trace,我有一个由2个微服务组成的应用程序 是否有一种方法可以在代码更改最少的情况下获得每个微服务接收和发送的每个请求的请求和响应 应用程序位于node.js中 我尝试过一些跟踪框架,但它们只提供时间信息 如果您使用express实现API后端服务器,则可以使用express winston截取每个调用的请求和响应 在下面的示例中,使用express winston将请求和响应注销到控制台 const winston = require('winston'); const expressWinston

我有一个由2个微服务组成的应用程序

是否有一种方法可以在代码更改最少的情况下获得每个微服务接收和发送的每个请求的请求和响应

应用程序位于node.js中


我尝试过一些跟踪框架,但它们只提供时间信息

如果您使用express实现API后端服务器,则可以使用express winston截取每个调用的请求和响应

在下面的示例中,使用express winston将请求和响应注销到控制台

const winston = require('winston');
const expressWinston = require('express-winston');

/**
 * Custom Wiston Express Middleware to log all requests and responses in the console.
 */
module.exports = async() => {
    // Creating middleware
    expressWinston.requestWhitelist.push('body');
    expressWinston.responseWhitelist.push('body');
    const wistonMiddleware = expressWinston.logger({
        transports: [
            new winston.transports.Console({
                json: true,
                colorize: true
            })
        ],
        // optional: control whether you want to log the meta data about the request (default to true)
        meta: true,
        // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}"
        msg: 'HTTP {{req.method}} {{req.url}}',
        // Use the default Express/morgan request formatting, with the same colors.
        // Enabling this will override any msg and colorStatus if true. Will only output colors on transports with colorize set to true
        expressFormat: true,
        // Color the status code, using the Express/morgan color palette (default green, 3XX cyan, 4XX yellow, 5XX red). Will not be recognized if expressFormat is true
        colorStatus: true,
        ignoreRoute: function (req, res) {
            return false;
        } // optional: allows to skip some log messages based on request and/or response
    });

    return wistonMiddleware;
};
但是,您可以使用其他winston模块以与弹性搜索不同的方式登录: