Last active
December 6, 2018 12:36
-
-
Save puzpuzpuz/4907d0636e88eaf316bc6f5445758c7c to your computer and use it in GitHub Desktop.
A snippet for Medium post: https://medium.com/@apechkurov/request-id-tracing-in-node-js-applications-c517c7dab62d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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