Last active
May 17, 2024 03:15
-
-
Save butaji/f0bf681228693aaceac2 to your computer and use it in GitHub Desktop.
API and Backend services part for Correlation Token example https://medium.com/@butaji/practical-microservices-correlation-tokens-75888baa5182
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
const getUserId = () => Math.floor((Math.random() * 1000000) + 1); //yeah, it should be real user id | |
const myLogger = (req, res, next) => { | |
const token = req.method + "_" + req.path + "_" + getUserId() + "_" + new Date().getTime(); | |
console.log(token); | |
req["app_token"] = token; | |
next(); | |
}; | |
app.get('/products', (req, res) => { | |
getProducts(req["app_token"], (d) => res.send(d)); | |
}); | |
const getProducts = (t, cb) => { | |
request('http://localhost:3001/products?token=' + t, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
cb(body); | |
} | |
}); | |
} | |
//GET_/products_114582_1454235210155 | |
//GET_/products_956499_1454235211210 |
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
const myLogger = (req, res, next) => { | |
console.log(req.query.token); | |
next(); | |
}; | |
app.use(myLogger); | |
//GET_/products_114582_1454235210155 | |
//GET_/products_956499_1454235211210 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment