Explore Related Tools
Hand-picked utilities and calculators related to this tool.
Express is essentially a series of **middleware** function calls. Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle.
app.use((req, res, next) => {
console.log('1. Middleware ran');
next(); // Passes control to next handler
});
app.get('/', (req, res) => {
console.log('2. Route handler ran');
res.send('Done'); // Ends the cycle
});If you don't call next(), the request will hang and the client will timeout. The only alternative to calling next() is to send a response back using res.send(), res.json(), etc.
Modern Express supports async middleware functions. This is perfect for checking a database or calling an external API before a request reaches the route.
app.use(async (req, res, next) => {
const user = await User.findById(req.session.userId);
req.user = user;
next();
});Hand-picked utilities and calculators related to this tool.
