Complete the code to send a simple text response using Express.
app.get('/', (req, res) => { res.[1]('Hello World!'); });
The res.send method sends a simple response to the client. It can send strings, buffers, or objects.
Complete the code to send a JSON response using Express.
app.get('/data', (req, res) => { res.[1]({ message: 'Success' }); });
The res.json method sends a JSON response. It automatically sets the Content-Type header to application/json.
Fix the error in sending a response with status 404 and a message.
app.get('/notfound', (req, res) => { res.status(404).[1]('Page not found'); });
To send a status code with a message, chain res.status() with res.send(). The res.send method sends the response body.
Fill both blanks to send a JSON response with status 201 and a message.
app.post('/create', (req, res) => { res.[1](201).[2]({ success: true }); });
First set the status code with res.status(), then send JSON data with res.json().
Fill the blanks to send a plain text response with status 500 and a custom message.
app.get('/error', (req, res) => { res.[1](500).[2]('Error: Something went wrong'); });
Set the status with res.status(), then send the plain text message using res.send().