Performance: Creating documents
This affects server response time and how quickly the client receives the document content.
Jump into concepts and practice - no test required
app.get('/doc', async (req, res) => { const doc = await generateLargeDocumentAsync(); res.send(doc); });
app.get('/doc', (req, res) => {
const doc = generateLargeDocumentSync();
res.send(doc);
});| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous document creation | N/A (server-side) | N/A | N/A | [✗] Bad |
| Asynchronous document creation | N/A (server-side) | N/A | N/A | [✓] Good |
get, post, put, and delete to handle HTTP requests.post method is used to create new documents or data entries.req.body?express.json() middleware to parse JSON request bodies.app.use(express.json()); to enable JSON parsing.app.post('/items', (req, res) => {
// Assume document creation here
res.status(201).send('Created');
});res.status(201), so it sends status 201.app.post('/users', (req, res) => {
const user = req.body;
saveUser(user);
res.send('User created');
});req.body will be undefined.app.use(express.json()), so req.body won't work.app.post and accesses req.body which is correct for creating a product.product.id = Date.now() simulates creating an ID. Responding with res.status(201).json(product) sends the created product with proper status.