Third-Party Middleware
Third-party middleware are Express middleware functions published by the community as npm packages. They add features Express does not include, such as security headers, cross-origin access, request logging, cookie parsing, compression and rate limiting. You install them with npm, require them and register them with app.use(), exactly like your own middleware.
Installing and registering packages
npm install helmet cors morgan cookie-parser compression express-rate-limit
const express = require('express');
const helmet = require('helmet');
const cors = require('cors');
const morgan = require('morgan');
const cookieParser = require('cookie-parser');
const compression = require('compression');
const { rateLimit } = require('express-rate-limit');
const app = express();
app.use(helmet()); // security headers
app.use(cors({ origin: 'https://app.example.com', credentials: true }));
app.use(morgan(process.env.NODE_ENV === 'production' ? 'combined' : 'dev'));
app.use(compression()); // gzip responses
app.use(express.json());
app.use(cookieParser());
const loginLimiter = rateLimit({ windowMs: 15 * 60 * 1000, limit: 20 });
app.post('/api/login', loginLimiter, (req, res) => {
res.json({ ok: true });
});
app.listen(3000);
Notice that loginLimiter is passed only to the login route. Middleware can be global, applied with app.use(), or local to one route or router.
What the popular packages do
| Package | Purpose |
|---|---|
helmet | Sets HTTP headers that reduce common attacks, such as content security policy and clickjacking protection. |
cors | Adds the headers that let a browser on another origin call your API. |
morgan | Logs each request's method, path, status and response time. |
cookie-parser | Reads the Cookie header into req.cookies, with optional signing. |
compression | Compresses response bodies to reduce transfer size. |
express-rate-limit | Limits how many requests one client can make in a time window. |
multer | Handles multipart/form-data file uploads. |
Understanding CORS errors
CORS confuses many beginners. The error appears in the browser, not the server, because the browser refuses to hand a cross-origin response to your JavaScript unless the server allows it. Calling cors() with no options allows every origin, which is fine for a public read-only API but too loose for one that uses cookies. List your front-end origin explicitly, and set credentials: true only if you send cookies. Note that CORS is a browser rule; it does not stop tools like curl from calling your API, so it is not a replacement for authentication.
Choosing packages wisely
Every dependency is code you trust with your requests. Before installing, check that the package is actively maintained, has clear documentation and is widely used. Read what it actually does: some middleware change req or res in ways other code depends on. Pin versions in package.json and run npm audit occasionally to spot known vulnerabilities.
Mistakes with ordering and scope
Middleware run top to bottom, so order changes behaviour. Put helmet and cors early so their headers are set even when a later route errors. Place the logger before routes, or requests that a route answers will never be logged. A rate limiter registered after express.static() will not count static file requests, which may be what you want; registered before, it counts every image too.
Another trap is applying heavy middleware globally when only one route needs it. File upload handling with multer belongs on the upload route, not on every request.
Points to remember
Third-party middleware extend Express through the same app.use() chain as built-in ones. Start with helmet, a correctly configured cors and a logger, add parsers and limits where needed, scope heavy middleware to the routes that use them, and pick well-maintained packages.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.