Skip to content

Instantly share code, notes, and snippets.

@anujtewari
Created June 20, 2020 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anujtewari/9a702f06fe4771d7f6ee6b16e0c3f8ce to your computer and use it in GitHub Desktop.
Save anujtewari/9a702f06fe4771d7f6ee6b16e0c3f8ce to your computer and use it in GitHub Desktop.
simple node app
index.js
const express = require('express')
const logger = require('./logger')
const common = {}
common.app = () => {
const app = express()
app.disable('x-powered-by')
return app
}
const app = common.app()
app.use((req, res, next) => {
req.locals = req.locals || {}
next()
})
app.use(logger.router)
app.get('/', function (req, res) {
res.send('Hello World')
})
app.listen(3000)
================================================================================================
logger.js
const logFuncToOverride = ['debug', 'error', 'log', 'info', 'warn']
const _ = require('underscore')
const url = require('url')
const MISSING = 'MISSING'
const COOKIE = 'COOKIE'
const BEARER_TOKEN = 'BEARER_TOKEN'
const LOG_LEVEL = 'log_level'
module.exports = {
MISSING,
COOKIE,
BEARER_TOKEN,
LOG_LEVEL: LOG_LEVEL,
router: (req, res, next) => {
try {
let reqid
let clientid
_.find(
req.query,
(val, key) => {
if (reqid && clientid) {
return true
}
if (!key) {
return false
}
switch (key.toLowerCase()) {
case 'reqid':
reqid = req.query[key]
break
case 'clientid':
clientid = req.query[key]
break
}
}
)
req.console = req.console || {}
logFuncToOverride.forEach((funcName) => {
req.console[funcName] = console[funcName].bind(console, `reqid="${reqid || MISSING}" clientid="${clientid} || MISSING}"`)
})
const urlObj = url.parse(req.url, true)
req.locals = req.locals || {}
req.locals.clientid = clientid || MISSING
req.locals.reqPathname = urlObj.pathname
let authorizationType = MISSING
if (req.headers.cookie && req.headers.cookie.length > 50) {
authorizationType = COOKIE
} else if (req.headers.authorization && req.headers.cookie.length > 20) {
authorizationType = BEARER_TOKEN
}
req.locals.authorization_type = authorizationType
req.console.log('REQUEST_STARTED',
`req_method="${req.method}"`,
`req_url="${req.url}"`,
`req_pathname="${urlObj.pathname}"`,
`req.header_client="${req.headers.client || MISSING}"`,
`req_locals_authorization_type="${req.locals.authorization_type}"`,
`req_headers_authorization_has_value="${!!req.headers.authorization}"`,
`req_headers_authorization_length="${!!req.headers.authorization && req.headers.authorization.length}"`,
`req_headers_cookie_has_value="${!!req.headers.cookie}"`,
`req_headers_cookie_length="${req.headers.cookie && req.headers.cookie.length}"`,
`req_local_address="${req.socket.localAddress}"`,
`req_local_port="${req.socket.localPort}"`
)
} catch (err) {
req.console.error('Error while extracting reqid and clientid. err', err)
} finally {
next()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment