Last active
July 18, 2017 05:04
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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