BackendStack21 / restana

Restana is a lightweight and fast Node.js framework for building RESTful APIs.
MIT License
467 stars 27 forks source link

Async middleware support #47

Closed Akkuma closed 5 years ago

Akkuma commented 5 years ago

By returning the result, middleware can now be properly asynchronous throughout the project, so you can write code like this:

async (req, res, next) => {
  const now = new Date().getTime()
  await next();
  console.log(new Date() - now);
}
jkyberneees commented 5 years ago

Hi @Akkuma , thanks for taking time on restana. I am afraid to say that your fix won't work as expected. Here I expose several reasons:

However, you can benefit from the on response event to get the same functionality:

service.use((req, res, next) => {
  const now = new Date().getTime()

  res.on('response', ({ res }) => {
    res.setHeader('x-response-time', new Date().getTime() - now)
  })

  return next()
})

You are required to use req.send(...) to get this event triggered.

Looking forward to your feedback, Thanks and Regards