mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Authetication, logger middleware, db create tool
This commit is contained in:
32
middlewares/reqlogger.middleware.js
Normal file
32
middlewares/reqlogger.middleware.js
Normal file
@@ -0,0 +1,32 @@
|
||||
const logger = require('../utils/logger.js')
|
||||
|
||||
// TODO: use this middleware in all modules
|
||||
|
||||
module.exports = function (options) {
|
||||
const loggableKeywords = options ? options.loggableKeywords : undefined
|
||||
|
||||
return function (req, res, next) {
|
||||
res.on('finish', function () {
|
||||
if (req.url.includes('_next/static')) {
|
||||
return
|
||||
}
|
||||
|
||||
const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
|
||||
const hostname = req.hostname.replace('www.', '').split('.')[0]
|
||||
// TODO: merge req.url and req.hostname checking
|
||||
// TODO: regexp includes checking
|
||||
let toLog = loggableKeywords && loggableKeywords.some((x) => {
|
||||
return req.url.includes(x)
|
||||
})
|
||||
|
||||
if (hostname.includes('dataeditor')) {
|
||||
toLog = true
|
||||
}
|
||||
|
||||
logger.LogReq(req, true, res.statusCode)
|
||||
if (toLog) { logger.LogReq(req) }
|
||||
if (res.statusCode !== 404) { logger.LogStat(req.url, ip) }
|
||||
})
|
||||
next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user