Performance: req.method and req.url
These properties affect how the server processes requests but have minimal impact on frontend page speed or rendering.
app.get('/data', async (req, res) => { const data = await fetchDataAsync(); res.send(data); });
app.use((req, res, next) => {
if (req.method === 'GET' && req.url === '/data') {
// heavy synchronous processing
for (let i = 0; i < 1e7; i++) {}
res.send('Data');
} else {
next();
}
});| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using req.method and req.url for routing | 0 (server-side) | 0 (server-side) | 0 (server-side) | [OK] Good |
req.method represent in an Express request?req.methodreq.method holds the HTTP method used by the client to make the request, such as GET, POST, PUT, DELETE.req.url gives the path and query, IP address is separate, and response status is not part of the request object.req.method, so we compare it to the string 'POST'.req.method to 'POST', which is correct. if (req.method = 'POST') uses assignment (=) which is wrong./home?user=123?
app.use((req, res, next) => {
console.log(req.method);
console.log(req.url);
next();
});req.method will be 'GET'.req.url includes the path and query string, so it will be '/home?user=123'.app.use((req, res, next) => {
if (req.method = 'POST') {
console.log('POST request received');
}
next();
});= which assigns 'POST' to req.method instead of comparing it. This causes a bug.=== to compare values in JavaScript, so the condition should be req.method === 'POST'./api/data with a GET method. Which code snippet correctly implements this?req.method is exactly 'GET' and req.url is exactly '/api/data'.Pass the test and this pattern moves unknown