Performance: Third-party middleware installation
This affects the server response time and the overall latency before the client receives the response.
const express = require('express'); const app = express(); const heavyMiddleware = require('heavy-middleware'); app.get('/special', heavyMiddleware(), (req, res) => res.send('Special route')); app.get('/', (req, res) => res.send('Hello')); app.listen(3000);
const express = require('express'); const app = express(); const heavyMiddleware = require('heavy-middleware'); app.use(heavyMiddleware()); app.get('/', (req, res) => res.send('Hello')); app.listen(3000);
| Pattern | Middleware Usage | Request Latency | Server Load | Verdict |
|---|---|---|---|---|
| Global heavy middleware | All routes | High (50-100ms added) | High | [X] Bad |
| Route-specific heavy middleware | Only needed routes | Low | Moderate | [OK] Good |
cors middleware in an Express app?import cors from 'cors'; is correct.app.use(cors()); not just cors.import express from 'express';
import bodyParser from 'body-parser';
const app = express();
app.use(bodyParser.json());
app.post('/data', (req, res) => {
res.send(req.body.name);
});
app.listen(3000);req.body to the parsed object.req.body.name, so the response is the 'name' value from JSON.helmet middleware:import express from 'express';
import helmet from 'helmet';
const app = express();
app.use(helmet);
app.get('/', (req, res) => res.send('Hello'));
app.listen(3000);app.use(helmet()); not app.use(helmet);.morgan logging middleware only for POST requests in your Express app. Which code correctly applies this?req.method.Pass the test and this pattern moves unknown