Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
'use strict'
// a primitive logger object
const logger = {
info: console.log
}
const express = require('express')
const uuidv4 = require('uuid/v4')
const app = express()
app.get('/', function (req, res) {
req._id = uuidv4(); // generate and store a request id
logger.info(`Started request handling, request id: ${req._id}`)
// pass `req` object into the nested call
fakeDbAccess(req)
.then((result) => res.json(result))
})
async function fakeDbAccess (req) {
return new Promise((resolve) => {
setTimeout(() => {
logger.info(`Logs from fakeDbAccess, request id: ${req._id}`)
resolve({ message: 'Hello world' })
}, 0)
})
}
app.listen(3000, (err) => {
if (err) {
logger.err('The app could not start')
}
logger.info('The app is listening on 3000')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment