Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to log request and response urls? #48

Closed
kimmobrunfeldt opened this issue Jan 20, 2016 · 12 comments
Closed

How to log request and response urls? #48

kimmobrunfeldt opened this issue Jan 20, 2016 · 12 comments

Comments

@kimmobrunfeldt
Copy link

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.
@chimurai
Copy link
Owner

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

@chimurai
Copy link
Owner

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.

@kimmobrunfeldt
Copy link
Author

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

@chimurai
Copy link
Owner

@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.

@mgerring
Copy link

mgerring commented Dec 4, 2020

@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?

@zanona
Copy link

zanona commented Feb 10, 2021

I have to agree with @mgerring as the current behaviour seems a bit confusing. Ideally I would like to simply obtain the final url requested, however both onProxyReq and onProxyRes, have host and url as an empty string with the only available field being path?

@materazu
Copy link

+1

@TheGooner93
Copy link

TheGooner93 commented Apr 14, 2021

@chimurai do we have a way to resolve this ?

@materazu
Copy link

materazu commented Apr 14, 2021

For me, the only solution was to concatenate manually the information

console.log('[HPM][FWD] ', req.method, req.path, '-- TO -->', `${process.env.API_URL}${req.path}`, 'with params', JSON.stringify(req.query));

Bad solution but solution :/

@TheGooner93
Copy link

@materazu I get what you have tried, but this should be something that proxyReq provides. It contains the context as the calls are going through, but the callback is somehow not aware of it?

@materazu
Copy link

I think too, the callback don't give us the context at this time, it's a good point to search why. If i have time to find this in code, i try to fix it.

@chimurai
Copy link
Owner

Think with onProxyRes you should be able to get the logging you want:

  onProxyRes: (proxyRes, req, res) => {
    // log original request and proxied request info
    const exchange = `[${req.method}] [${proxyRes.statusCode}] ${req.path} -> ${proxyRes.req.protocol}//${proxyRes.req.host}${proxyRes.req.path}`;
    console.log(exchange); // [GET] [200] / -> http://www.example.com
  },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants