Performance: res.send for general responses
This affects server response time and client perceived loading speed by controlling how quickly the server sends data to the browser.
app.get('/data', async (req, res) => { const data = await fetchDataAsync(); res.json(data); });
app.get('/data', (req, res) => {
const data = fetchDataSync();
res.send(JSON.stringify(data));
});| Pattern | Server Blocking | Response Size | Network Delay | Verdict |
|---|---|---|---|---|
| res.send with sync fetch and manual JSON | High (blocks event loop) | Medium (stringified JSON) | Medium | [X] Bad |
| res.json with async fetch | Low (non-blocking) | Medium (automatic JSON) | Low | [OK] Good |
res.send() do in an Express app?res.send()res.send() is used to send a response back to the client in Express.res.send() automatically ends the response, so no more data is sent.res.send?res.send is as a function with parentheses and the data inside.res.send('Hello'); which is correct. res.send({ message: 'Hello' }); sends an object, which is valid but not plain text. Options B and D have invalid syntax.app.get('/test', (req, res) => {
res.send({ success: true });
});res.send() receives an object, Express converts it to JSON automatically.app.get('/hello', (req, res) => {
res.send('Hello');
res.send('Again');
});res.send() is called, the response is finished and cannot be sent again.res.send() a second time causes an error because the response is already closed.res.send. Which code correctly does this?res.status(201) to set the HTTP status code to 201..send({ message: 'Created' }) to send the JSON response.Pass the test and this pattern moves unknown