Complete the code to send a simple text response using Express.
app.get('/', (req, [1]) => { [1].send('Hello World!'); });
The res object is used to send the response back to the client in Express.
Complete the code to set the HTTP status code to 404 before sending the response.
app.get('/notfound', (req, res) => { res.status([1]).send('Page not found'); });
HTTP status code 404 means 'Not Found'. Setting it before sending the response tells the client the page was not found.
Fix the error in the code to properly send a JSON response.
app.get('/data', (req, [1]) => { [1].json({ message: 'Hello' }); });
The callback must use res as the second parameter to call res.json().
Fill both blanks to set a custom header and send a plain text response.
app.get('/custom', ([1], [2]) => { [2].set('X-Custom-Header', '12345'); [2].send('Custom header set'); });
The first parameter is the request object (req), the second is the response object (res), which is used to set headers and send responses.
Fill all three blanks to send a JSON response with status 201 and a custom header.
app.post('/create', ([1], [2]) => { [2].status([3]).set('Location', '/new-item').json({ success: true }); });
req and res parameters.The first parameter is req, the second is res. Status code 201 means 'Created'. We set a 'Location' header and send JSON.