Skip to content

Instantly share code, notes, and snippets.

@butaji
Last active May 17, 2024 03:15
Show Gist options
  • Save butaji/f0bf681228693aaceac2 to your computer and use it in GitHub Desktop.
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
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
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