Skip to content

Instantly share code, notes, and snippets.

@ChristianRich
Last active July 18, 2017 05:04
Show Gist options
  • Save ChristianRich/5a95d8ee4e9eb1153548020fbd1dfe52 to your computer and use it in GitHub Desktop.
Save ChristianRich/5a95d8ee4e9eb1153548020fbd1dfe52 to your computer and use it in GitHub Desktop.
Intercept all HTTPx traffic for logging and debugging purposes. This snippet should be a part of your server startup script.
import * as httpInterceptor from 'global-request-logger';
const debug = require('debug')('my:app');
if(process.env.DEBUG){
httpInterceptor.initialize();
httpInterceptor.on('success', (req, res) => {
const protocol = (req.port === 443 ? 'https://' : 'http://');
debug(`/${req.method} ${protocol}${req.host}${req.path}`);
})
httpInterceptor.on('error', (req, res) => {
const protocol = (req.port === 443 ? 'https://' : 'http://');
debug(`/${req.method} ${protocol}${req.host}${req.path}`);
})
}
@ChristianRich
Copy link
Author

ChristianRich commented Jul 18, 2017

Sometimes I have the need to see what outgoing HTTPx connections my Node application is making - weather it be from my code or from a 3rd party npm module. This snippet intercepts all HTTPx traffic and logs it.

Run your app using the DEBUG env variable e.g:
$ DEBUG=* node index
$ DEBUG=my:app* node index

You should now see HTTPx requests logged as e.g:
my:app /PUT https://s3-us-west-2.amazonaws.com/example/54088_2017-07-02T11%3A42%3A31.000Z.xml +0ms

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