Skip to content

Instantly share code, notes, and snippets.

@ceoworks
Last active May 7, 2018 16:56
Show Gist options
  • Select an option

  • Save ceoworks/0ae79cc8e5da611da01c87c5110e5bea to your computer and use it in GitHub Desktop.

Select an option

Save ceoworks/0ae79cc8e5da611da01c87c5110e5bea to your computer and use it in GitHub Desktop.
Головоломочка по хранению RequestId
// одна из первых миддлварок
module.exports = (ctx, next) => {
ctx.requestId = uuid.v4();
next();
};
// route
router.post('/pay', (ctx, next) => {
global.requestId = ctx.requestId; // либо сохранить в глобал - не подходит, перезапишется следующими запросами, будет путаница
// либо передавать ctx по всему приложению - плохо ctx таскать везде
service.processPay(user, {requestId}); // либо передавать вручную requestId - плохо, много рутины, человеческого фактора - забыли и т.п.
});
// пример отправки запроса где-то в сервисах
const {requestId} = global; // global надо заменить каким-то хранилищем, которое будет существовать в рамках одного request'a
request.post({headers: {requestId}, url: '', json: {}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment