feat: console log incoming requests on dev mode

This commit is contained in:
Bobby 2022-09-21 07:44:40 +07:00
parent bf077f4aa3
commit ffe6d6ed88
No known key found for this signature in database
GPG Key ID: 941839794CBF5A09
2 changed files with 31 additions and 0 deletions

View File

@ -0,0 +1,24 @@
const logger = require('./../../logger')
class RequestLogging {
constructor () {
logger.log('Initiated RequestLogging middleware.')
}
#middleware (req, res, next) {
req.locals.debug = {
ip: req.ip,
method: req.method,
path: req.path,
path_parameters: req.path_parameters
}
logger.log(`Incoming from ${req.locals.debug.ip} -> ${req.locals.debug.method} ${req.locals.debug.path}`)
return next()
}
get middleware () {
return this.#middleware.bind(this)
}
}
module.exports = RequestLogging

View File

@ -55,6 +55,7 @@ paths.initSync()
const utils = require('./controllers/utilsController')
// Middlewares
const DebugLogging = require('./controllers/middlewares/DebugLogging')
const ExpressCompat = require('./controllers/middlewares/ExpressCompat')
const NunjucksRenderer = require('./controllers/middlewares/NunjucksRenderer')
const RateLimiter = require('./controllers/middlewares/RateLimiter')
@ -71,6 +72,12 @@ const file = require('./routes/file')
const nojs = require('./routes/nojs')
const player = require('./routes/player')
// Incoming requests logging (development mode)
if (utils.devmode) {
const DebugLoggingInstance = new DebugLogging()
safe.use(DebugLoggingInstance.middleware)
}
// Express-compat
const expressCompatInstance = new ExpressCompat()
safe.use(expressCompatInstance.middleware)