Performance: POST route handling
This affects server response time and user experience by how quickly the server processes and responds to POST requests.
app.use(express.json()); app.post('/submit', (req, res) => { const data = req.body; // process data res.send('Done'); });
app.post('/submit', (req, res) => { let body = ''; req.on('data', chunk => { body += chunk; }); req.on('end', () => { const data = JSON.parse(body); // process data res.send('Done'); }); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual body parsing in route | N/A | N/A | N/A | [X] Bad |
| Using express.json() middleware | N/A | N/A | N/A | [OK] Good |
app.post method in Express do?app.get and app.post to handle different HTTP request types.app.postapp.post specifically handles POST requests, which are used to send data to the server./submit?app.post and path '/submit'.(req, res) in that order, and the response should be sent inside the function body.{"name":"Alice"} is sent to /hello?
const express = require('express');
const app = express();
app.use(express.json());
app.post('/hello', (req, res) => {
res.send(`Hello, ${req.body.name}!`);
});app.use(express.json()) which parses JSON bodies into req.body.name property from req.body. Since the body has {"name":"Alice"}, it will respond with "Hello, Alice!".app.post('/data', (req, res) => {
console.log(req.body);
res.send('Data received');
});
Assuming the client sends JSON data in the request body.app.use(express.json()), which is needed to parse JSON request bodies into req.body.req.body will be undefined, so logging it won't show the sent data./register that accepts JSON data with username and password. It should respond with { success: true, user: username }. Which code correctly implements this?express.json() as middleware for this route, so req.body will be parsed correctly.username and password from req.body and responds with JSON using res.json(), which is correct.Pass the test and this pattern moves unknown