Skip to content

Instantly share code, notes, and snippets.

@puzpuzpuz
Last active December 6, 2018 12:36
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 puzpuzpuz/4907d0636e88eaf316bc6f5445758c7c to your computer and use it in GitHub Desktop.
Save puzpuzpuz/4907d0636e88eaf316bc6f5445758c7c to your computer and use it in GitHub Desktop.
'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