Skip to content

Instantly share code, notes, and snippets.

@evanshortiss
Created October 25, 2017 00:32
Show Gist options
  • Select an option

  • Save evanshortiss/2fc348fa70297595c4430c3b0da8346f to your computer and use it in GitHub Desktop.

Select an option

Save evanshortiss/2fc348fa70297595c4430c3b0da8346f to your computer and use it in GitHub Desktop.
Simple middleware to log information related to the underlying socket and proxies that forwarded the incoming request
// Set a DEBUG environment var to "*" or "socket-logger" to enable
const log = require('debug')('socket-logger')
// Place this before other middlewares so it is invoked first - remember they invoke in the defined order
app.use(function (req, res, next) {
log('============================================================')
log(`received request from remote socket with req.headers - ${JSON.stringify(req.headers)}`)
log(`received request from remote socket with req.socket.address - ${JSON.stringify(req.socket.address())}`)
log(`received request from remote socket with req.socket.localAddress - ${req.socket.localAddress}`)
log(`received request from remote socket with req.socket.localPort - ${req.socket.localPort}`)
log(`received request from remote socket with req.socket.remoteFamily - ${req.socket.remoteFamily}`)
log(`received request from remote socket with req.socket.remotePort - ${req.socket.remotePort}`)
log(`received request from remote socket with req.socket.remoteAddress - ${req.socket.remoteAddress}`)
log('============================================================')
// carry on with request processing...
next()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment