better demonstrate middleware flow#1195
better demonstrate middleware flow#1195dead-horse merged 1 commit intokoajs:masterfrom jasonmacgowan:jasonmacgowan-patch-1
Conversation
It's not incredibly clear from this example that middleware can act on context changes from other middleware. This updates the logger middleware to use the calculated response time value from the response time middleware instead of calculating it twice.
Codecov Report
@@ Coverage Diff @@
## master #1195 +/- ##
=======================================
Coverage 99.73% 99.73%
=======================================
Files 5 5
Lines 374 374
=======================================
Hits 373 373
Misses 1 1Continue to review full report at Codecov.
|
|
I'm +/-0 on this. |
|
I get where you're headed on this, and I dig it, but wouldn't this make the |
|
@doug-wade measured in milliseconds in this simple of an example I'd be surprised if there was a difference. If there was a difference, we are already logging one value and sending back another so I'd imagine it's a non-issue. |
| const ms = Date.now() - start; | ||
| ctx.set('X-Response-Time', `${ms}ms`); | ||
| const rt = ctx.response.get('X-Response-Time'); | ||
| console.log(`${ctx.method} ${ctx.url} - ${rt}`); |
There was a problem hiding this comment.
it's not good here because rt is a string
There was a problem hiding this comment.
@dead-horse I'm not sure what you mean; can you elaborate please? Thanks!
There was a problem hiding this comment.
never mind, my misunderstanding.
The way this example is written makes it seem like middleware can't act on the context changes of other middleware. This updates the logger middleware to use the calculated response time value from the response time middleware instead of calculating it itself.