Performance: Documenting endpoints
This affects the server response time and client load speed by adding metadata and documentation overhead.
const swaggerDoc = require('./swagger.json'); const swaggerUi = require('swagger-ui-express'); // Serve docs separately app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDoc)); app.get('/api/data', (req, res) => { const data = fetchData(); res.json(data); });
app.get('/api/data', (req, res) => { // heavy inline documentation strings /** * This endpoint returns data with detailed explanation... * Lots of comments and examples inside the handler */ const data = fetchData(); res.json(data); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Inline documentation in handlers | N/A | N/A | N/A | [X] Bad |
| Separate static docs route | N/A | N/A | N/A | [OK] Good |
// for single-line comments and /* */ for multi-line comments.// is correct for single-line comments. // Returns user info by ID
app.get('/user/:id', (req, res) => {
const id = req.params.id;
res.send(`User ID is ${id}`);
});
What will the server respond with when a client requests /user/42?:id as a parameter, which is accessed via req.params.id./user/42, req.params.id is '42', so the response is 'User ID is 42'. // Get all products
app.get('/products', (req, res) => {
res.send(productsList);
});
But when running, it throws an error: ReferenceError: productsList is not defined.
What is the likely fix?productsList is not defined, meaning the variable is missing or not declared.productsList before the route uses it, so the route can send it properly.productsList before using it in the route -> Option APass the test and this pattern moves unknown