Why calling next() in express routes is optional? Why calling next() in express routes is optional? express express

Why calling next() in express routes is optional?


The true callback in an Express middleware chain isn't actually next, as you might assume, but res.end(). Any request that is handled must, at some point, call this function (either directly or indirectly) to say that the request has been handled.

When you call next() in your middelware, you're passing on the responsibility of calling res.end() to the middleware further down the middleware chain. If the last middleware in the chain calls next(), then Express knows that no middleware has or will handle the request, and so a "404 Not Found" page will be generated.


Also, calling next(err) with a single argument err works differently, because it tells Express that an error occured and that it should instead by handled by the error handler.


next is used to pass the request along to other (possible) handlers. If you're writing a middleware or (less likely) a route handler that doesn't want to handle the request, you call next and let Express sort it out. Perhaps there is other middleware, or another route handler, that can handle it.

If your code does handle the request, meaning that it sends back a response, you shouldn't call next. Express doesn't care about that: internally, middleware and route handlers are stored in an array, and Express checks each item to see if it can handle the request. It passes the next function so that it can be told to try the next matching handler. But it's optional.

It's also used to pass errors to Express, so it can pass them along the global error handler. That's what happens when you see next(err) being called.


next is called when you want to pass the control to the next middleware.

since the order declaration of the middleware is relevant, express will just take care of verify the arguments of your function, if it finds a third parameter then it will pass a pointer to to the subsequent middleware.

if you return the result to the client (eg. calling res.render) you don't need the next to be called hence you don't need next as the third argument.