Authetication, logger middleware, db create tool

This commit is contained in:
MrFry 2020-04-07 09:26:45 +02:00
parent 5f0b17a0db
commit ebd27f93c1
11 changed files with 164 additions and 94 deletions

View 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()
}
}