0
0
Expressframework~10 mins

Why understanding res matters in Express - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to send a simple text response using Express.

Express
app.get('/', (req, [1]) => {
  [1].send('Hello World!');
});
Drag options to blanks, or click blank then click option'
Ares
Bresult
CresponseObj
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'request' or 'result' instead of 'res' causes errors.
Trying to send response with 'req' instead of 'res'.
2fill in blank
medium

Complete the code to set the HTTP status code to 404 before sending the response.

Express
app.get('/notfound', (req, res) => {
  res.status([1]).send('Page not found');
});
Drag options to blanks, or click blank then click option'
A404
B200
C500
D301
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 means success, which is incorrect here.
Using 500 means server error, not 'not found'.
3fill in blank
hard

Fix the error in the code to properly send a JSON response.

Express
app.get('/data', (req, [1]) => {
  [1].json({ message: 'Hello' });
});
Drag options to blanks, or click blank then click option'
Aresponse
Brequest
Cresult
Dres
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different name than 'res' but calling 'res.json()' causes errors.
Swapping the order of parameters.
4fill in blank
hard

Fill both blanks to set a custom header and send a plain text response.

Express
app.get('/custom', ([1], [2]) => {
  [2].set('X-Custom-Header', '12345');
  [2].send('Custom header set');
});
Drag options to blanks, or click blank then click option'
Areq
Bres
Crequest
Dresponse
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of parameters.
Using the wrong variable name for the response object.
5fill in blank
hard

Fill all three blanks to send a JSON response with status 201 and a custom header.

Express
app.post('/create', ([1], [2]) => {
  [2].status([3]).set('Location', '/new-item').json({ success: true });
});
Drag options to blanks, or click blank then click option'
Areq
Bres
C201
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using status 200 instead of 201 for creation.
Swapping req and res parameters.