Consider this Express GET route handler:
app.get('/hello', (req, res) => {
res.send('Hello World!');
});What will the client receive when requesting /hello?
app.get('/hello', (req, res) => { res.send('Hello World!'); });
Look at what res.send does with a string argument.
The res.send method sends the string as the response body with status 200 by default.
Which of the following Express GET route definitions correctly captures a URL parameter named id?
URL parameters use a colon : before the name.
Option B uses /:id which correctly defines a required URL parameter named id.
Option B treats 'id' as a literal path segment, not a parameter.
Option B uses invalid syntax with angle brackets.
Option B defines an optional parameter with ?, which is valid but changes the route behavior.
Examine this Express GET route handler:
app.get('/data', (req, res) => {
const data = fetchData();
res.send(data);
});Assuming fetchData is an async function returning a Promise, why does this code cause an error or unexpected behavior?
app.get('/data', (req, res) => {
const data = fetchData();
res.send(data);
});Think about what happens when you call an async function without awaiting it.
Since fetchData returns a Promise, calling it without await means data is a Promise object. Sending this Promise directly sends the object, not the resolved value.
Consider this Express code:
let count = 0;
app.get('/count', (req, res) => {
count += 1;
res.send(`Count is ${count}`);
});What will the client see when requesting /count three times in a row?
let count = 0; app.get('/count', (req, res) => { count += 1; res.send(`Count is ${count}`); });
Think about how the variable count changes with each request.
The variable count is stored in the server's memory and increments by 1 on each request, so the response reflects the updated count.
Given these routes:
app.get('/item/new', handler2);
app.get('/item/:id', handler1);Which statement correctly describes how Express matches requests?
Think about route specificity and matching order in Express.
Express matches routes in order and prefers exact string matches over parameter matches when both could match the same path.