Tida Shih
Tida Shih

Reputation: 11

It doesn't show response data if I log http response in Express Gateway app

  1. I use express gateway to proxy http request.
  2. I make a plugin(middleware) to log http response.
  3. The log shows ExpressJS Response Object, but I can't find the data sent back from serviceEndpoints.

  4. I have tried to log "res.body", but it shows undefined

  5. This environment is docker container with node:8.9.4-alpine
  6. Express gateway version: "^1.9.1"

system.config.json


"plugins": {
    "logger": {
        "package": "logger"
    },

logger plugin:


    const stringify = require('json-stringify-safe');
    module.exports = {
      name: 'logger',
      init: function (pluginContext) {
        pluginContext.registerGatewayRoute(
          (gatewayExpressApp) => {
            gatewayExpressApp.use((req, res, next) => {
              res.on('finish', function () {
                console.log('response: %s', stringify(res));
              });
              next();
            });
          }
        );
      }
    };

The res object shows:

    {
        "domain": null,
        "_events": {
            "finish": [null, null, null]
        },
        "_eventsCount": 5,
        "output": [],
        "outputEncodings": [],
        "outputCallbacks": [],
        "outputSize": 0,
        "writable": true,
        "_last": true,
        "upgrading": false,
        "chunkedEncoding": false,
        "shouldKeepAlive": false,
        "useChunkedEncodingByDefault": false,
        "sendDate": true,
        "_removedConnection": false,
        "_removedContLen": false,
        "_removedTE": false,
        "_contentLength": null,
        "_hasBody": true,
        "_trailer": "",
        "finished": true,
        "_headerSent": true,
        "socket": null,
        "connection": null,
        "_header": "HTTP/1.1 200 OK\r\nx-powered-by: Express\r\ntimezone-offset: +0:0\r\ntimezone: UTC\r\ncontent-type: application/json; charset=utf-8\r\ncontent-length: 803\r\netag: W/\"323-lP/wh6xQz1k1vBMgrvl3whFu1vA\"\r\ndate: Thu, 20 Jun 2019 08:00:24 GMT\r\nconnection: close\r\n\r\n",
        "_sent100": false,
        "_expect_continue": false,
        "req": {
    ...
        }
        "locals": {},
        "statusCode": 200,
        "statusMessage": "OK"
    }

I want to see this

{
    "status": "success",
    "data": {
        "rows": [],
        "rowLength": 0,
        "pageState": null
    },
    "error": ""
}

Upvotes: 1

Views: 277

Answers (1)

Aytuğ Ulış
Aytuğ Ulış

Reputation: 21

I had the same problem. Here is the solution:

    const write = res.write;
    res.write = (data) => {
      res.data = JSON.parse(data);
      res.write = write;
      return res.write(data);
    };

    res.once("finish", () => {
      console.log(res.data);
    });

Upvotes: 0

Related Questions