The next()
function passes control to the next middleware function. If not called, the request will be left hanging. You can pass an error to next(err)
to trigger error handling middleware.
app.use((req, res, next) => {
if (req.headers.authorization) {
next(); // Continue to next middleware
} else {
next(new Error('Unauthorized')); // Trigger error handling
}
});