Last active
May 7, 2018 16:56
-
-
Save ceoworks/0ae79cc8e5da611da01c87c5110e5bea to your computer and use it in GitHub Desktop.
Головоломочка по хранению RequestId
This file contains hidden or 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
| // одна из первых миддлварок | |
| 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