Which of the following best explains why Express is widely used for building web servers in Node.js?
Think about what Express adds on top of Node.js to make web server coding easier.
Express is popular because it offers a simple way to handle routing and middleware, making Node.js web servers easier to build and maintain.
Consider this Express code snippet adding middleware. What is the behavior when a request matches the middleware?
app.use((req, res, next) => {
console.log('Middleware called');
next();
});What does calling next() inside middleware do?
Calling next() passes control to the next middleware or route handler, so the request continues processing after logging.
Which option shows the correct syntax to define a GET route for path '/hello' that sends 'Hi!' as response?
Remember the Express method to send a response body in one call.
Option A uses app.get with a callback that calls res.send, which is the correct and simplest way to send a response.
Given this Express code, what will be the final response sent?
app.use((req, res, next) => {
res.locals.value = 1;
next();
});
app.use((req, res, next) => {
res.locals.value += 2;
next();
});
app.get('/', (req, res) => {
res.send(`Value is ${res.locals.value}`);
});Think about how res.locals is shared across middleware in the same request.
res.locals is an object shared during the request. The first middleware sets value to 1, the second adds 2, so final value is 3.
Examine this Express app code. Why does it crash with a TypeError?
const express = require('express');
const app = express();
app.use(express.json());
app.get('/test', (req, res) => {
res.send('Test route');
});
app.listen(3000, () => {
console.log('Server started on port 3000');
});Consider the order of middleware and server start.
Middleware must be added before starting the server to handle requests properly. Adding express.json() after app.listen means it won't run on incoming requests, causing unexpected behavior.