Closed
Description
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 serverproxyReq.baseUrl
is not defined, I'm not sure what I should use to get the host part of the request.