A bit of internals here. The main purpose of express
app
handle
function is to send a response to the client, and terminate the request-response cycle
. And termination of this cycle can be done by one of the response methods
(e.g. res.end(), res.json(), etc). Meaning if a middleware or route handler
does some actions but then doesn't call one of the response methods
or pass the control to the next handler or middleware, the request-response cycle
will not be terminated. But what the next
does depends on where and how it gets called.
To manage different tasks (route handlers, middlewares) express
creates stacks
. They look like a queue
of the tasks
. Each router
and route
creates its own stack
of tasks
;
The use
method of the express
app
pushes task
(middleware
function) to the stack
of the router
. The app.get
, app.post
, etc creates a separate route
(with its own stack
, and pushes to it the actual handlers
of the route
) in the router
, then pushes to the router
wrapped in a function those route
handlers. Meaning when a route
gets created in the router
stack
something like route
task
(wrapper function) with subtasks
pushed.
// pushes task to the router stack
app.use((req, res, next) => {
console.log('log request');
next();
});
// creates route, route stack,
// pushes tasks to the route stack,
// wraps tasks in a function (another task)
// pushes wrapper function to the
// router stack
app.get('/', (req, res, next) => {
res.send('Hello World');
});
As a route
has its own stack
calling next
without the arguments gets us only to the next handler of the route
:
app.get('/',
(req, res, next) => {
console.log('first handler');
// passes the control to the second handler
next();
},
(req, res, next) => {
console.log('second handler');
res.send('Hello World');
}
);
Calling next
inside a middleware
(express
recommends to apply use
method for mounting a middleware
) gets us to the next route
or middleware
of the router
, cause middleware
(when mounted) was pushed to the router
stack
.
next
accepts different arguments. Any argument that is not 'route'
or 'router'
will be treated as an error and will be passed to the error
middleware
that must be mounted after all routes and have four arguments:
// error handling middleware
app.use((error, req, res, next) => {
res.status(error.status || 500);
res.send(error.message || 'Server internal error');
});
String 'route'
as an argument for next
will skip all remaining route
handlers
and gets us the the next route
of the router
:
app.get('/',
(req, res, next) => {
console.log('first handler');
// passes control to the next route
next('route');
},
(req, res, next) => {
// this handler will be skipped
next();
}
);
app.get('/',
(req, res, next) => {
// this route will be called at the end
res.send('Hello World');
}
);
String 'router'
as an argument for next
gets us out of the current router
:
// router 'one'
app.get('/',
(req, res, next) => {
console.log('first handler');
// passes control to the next router
next('router');
},
(req, res, next) => {
// this handler will be skipped
next();
}
);
// router 'two'
app.get('/',
(req, res, next) => {
// this route will be called at the end
res.send('Hello World');
}
);
res.send
to complete the request. If it doesn't exist, there is likely another handler that will issue an error and complete the request then.app.post('/login',function(req,res))
afterapp.get('/users',function(req,res))
it will call login being the next route in the app.js file by calling next()?app.get("/users")
, then it will be run if handler above calls next.next()
, just as a convention, but it could be called literally ANYTHING possible.