Skip to content

How to log request and response urls? #48

Closed
@kimmobrunfeldt

Description

@kimmobrunfeldt

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions