Node-restify: Ambiguous documentation about next() use

Created on 28 Apr 2016  路  3Comments  路  Source: restify/node-restify

In the docs sometimes just calling next(); is enough, and sometimes middleware/path handlers use return next();.

What is the difference between both? When one should be used vs the other?

Also, it says that next('somepath'); does a redirect, and in some places there is next(false); for - I think - signaling error? And sometimes it's next(someObj); - is this for errors too?

Docs

Most helpful comment

next() currently has four behaviors:

  • next(false) - stops processing handler chain immediately, regardless of whether or not response has been flushed.
  • next(err) - by default, will serialize the error, send it as a response AND stop processing the handler chain
  • next() - continue to next handler in the chain
  • next('redirect') - jump to another handler chain

All 3 comments

Returning next is a control flow concept. When you need to ensure nothing else gets run in your middleware you should return next when calling it, otherwise if it's at the end of your function it doesn't matter too much.

As far as the inconsistencies with passing data to next we are going to address that soon to reduce the confusion.

next() currently has four behaviors:

  • next(false) - stops processing handler chain immediately, regardless of whether or not response has been flushed.
  • next(err) - by default, will serialize the error, send it as a response AND stop processing the handler chain
  • next() - continue to next handler in the chain
  • next('redirect') - jump to another handler chain

Related: #1019

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FLYBYME picture FLYBYME  路  6Comments

nodesocket picture nodesocket  路  4Comments

0v3rst33r picture 0v3rst33r  路  6Comments

stevehipwell picture stevehipwell  路  8Comments

augustovictor picture augustovictor  路  4Comments