Http-proxy-middleware: How to log request and response urls?

Created on 20 Jan 2016  Â·  5Comments  Â·  Source: chimurai/http-proxy-middleware

I would like to log each request no matter what status code the target server responded. I'm trying to achieve the following format:

// Format:
// -->  [METHOD] [PATH_REQUESTED_FROM_PROXY] -> [URL_REQUESTED_FROM_TARGET]
// Example:
// -->  GET /v1/users/123 -> https://my-app.herokuapp.com/api/v1/users/123

My proxy options are:

var proxyOpts = {
    target: 'https://my-app.herokuapp.com/',
    pathRewrite: {
        '^/v1' : '/api/v1'
    },
    onProxyReq: function onProxyReq(proxyReq, req, res) {
        // Log outbound request to remote target
        console.log('-->  ', req.method, req.path, '->', proxyReq.baseUrl + proxyReq.path);
    },
    onError: function onError(err, req, res) {
        console.error(err);
        res.status(500);
        res.json({error: 'Error when connecting to remote server.'});
    },
    logLevel: 'debug',
    changeOrigin: true,
    secure: true
};

Which outputs:

-->   GET /api/v1/users/123 -> undefined/api/v1/users/123

Problems:

  • req.path is already converted to the new format with pathRewrite, I would want to log the url which client actually requested from this proxy server
  • proxyReq.baseUrl is not defined, I'm not sure what I should use to get the host part of the request.
enhancement question

Most helpful comment

I think will be nice to encode the proxy behavior in the log statements too;
Different arrows indicate what happend:

  • -> default.
  • ~> pathRewrite applied.
  • => proxyTable applied.
  • ≈> both pathRewrite and proxyTable applied.

So in your use-case; the original url will get logged, its target URI and the pathRewrite arrow.

[HPM] GET /v1/users/123 ~> https://my-app.herokuapp.com/api/v1/users/123

Let me know what you think.

All 5 comments

Unfortunately the req.path information is destroyed after it has been rewritten.

For host I'm using the req.hostname
https://github.com/chimurai/http-proxy-middleware/blob/v0.9.1/index.js#L71

I like your suggested improvements to the logging behaviour.
Think it should be part of the core functionality.

This statement can also be removed, if the original path can be logged:
https://github.com/chimurai/http-proxy-middleware/blob/v0.9.1/index.js#L59

I think will be nice to encode the proxy behavior in the log statements too;
Different arrows indicate what happend:

  • -> default.
  • ~> pathRewrite applied.
  • => proxyTable applied.
  • ≈> both pathRewrite and proxyTable applied.

So in your use-case; the original url will get logged, its target URI and the pathRewrite arrow.

[HPM] GET /v1/users/123 ~> https://my-app.herokuapp.com/api/v1/users/123

Let me know what you think.

Thanks for the quick response! If the default format would be that, I'd be happy! :) Looks good to me.

@kimmobrunfeldt, I just published v0.11.0 with the logging improvements. Can you verify this change?

Let me know if more improvements can be made in logging.

@chimurai sorry to raise this issue from the dead, but I'm wondering how to use this info in a test. I need to write a test to ensure that URLs are being rewritten correctly by the proxy, and I don't know how to get the rewritten URL from the request or response object, or, alternately, how I would use the log output of the proxy in a test. Any suggestions?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DylanPiercey picture DylanPiercey  Â·  6Comments

georgyfarniev picture georgyfarniev  Â·  7Comments

noahgrant picture noahgrant  Â·  4Comments

zh-h picture zh-h  Â·  3Comments

nikksan picture nikksan  Â·  3Comments