Third-party middleware helps add ready-made features to your Node.js app easily. It saves time by handling common tasks for you.
Third-party middleware usage in Node.js
import express from 'express'; import middlewareName from 'middleware-package'; const app = express(); app.use(middlewareName(options));
Use import to bring in the middleware package.
Call app.use() to add the middleware to your app.
morgan logger middleware to log requests in 'dev' format.import express from 'express'; import morgan from 'morgan'; const app = express(); app.use(morgan('dev'));
cors middleware to allow cross-origin requests.import express from 'express'; import cors from 'cors'; const app = express(); app.use(cors());
helmet middleware to improve security by setting HTTP headers.import express from 'express'; import helmet from 'helmet'; const app = express(); app.use(helmet());
This program uses the morgan middleware to log each request in a short format. When you visit the root URL, it responds with a greeting message.
import express from 'express'; import morgan from 'morgan'; const app = express(); // Use morgan middleware to log requests app.use(morgan('tiny')); app.get('/', (req, res) => { res.send('Hello from middleware example!'); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Always install the middleware package first using npm install package-name.
Middleware order matters: put middleware that parses data before routes that use that data.
Some middleware needs options to work properly; check the package docs.
Third-party middleware adds useful features without extra coding.
Use app.use() to add middleware to your Express app.
Pick middleware based on what your app needs, like logging, security, or parsing.
Practice
Solution
Step 1: Understand middleware role
Middleware adds features like logging or parsing to an app without extra coding.Step 2: Compare options
Only To add extra features without writing them from scratch correctly describes middleware's purpose; others are incorrect or unrelated.Final Answer:
To add extra features without writing them from scratch -> Option BQuick Check:
Middleware adds features [OK]
- Thinking middleware replaces Express
- Believing middleware runs without Node.js
- Confusing middleware with full app code
helmet to an Express app?Solution
Step 1: Recall Express middleware syntax
Middleware is added usingapp.use()with the middleware function called.Step 2: Check options
Only app.use(helmet()); usesapp.use(helmet());correctly; others have wrong syntax.Final Answer:
app.use(helmet()); -> Option DQuick Check:
Middleware added with app.use() [OK]
- Forgetting parentheses after middleware name
- Using incorrect method names like app.helmet()
- Passing middleware without calling it
const express = require('express');
const morgan = require('morgan');
const app = express();
app.use(morgan('tiny'));
app.get('/', (req, res) => {
res.send('Hello World');
});
app.listen(3000);Solution
Step 1: Understand morgan middleware
Morgan logs HTTP request info to the console when used with app.use().Step 2: Analyze code behavior
GET '/' triggers morgan logging and sends 'Hello World' response.Final Answer:
Logs request details in console and responds with 'Hello World' -> Option CQuick Check:
morgan logs + response sent [OK]
- Assuming morgan changes response content
- Thinking morgan causes errors if used like this
- Ignoring middleware effect on logging
cors middleware:
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors);
app.get('/', (req, res) => res.send('OK'));
app.listen(3000);Solution
Step 1: Check middleware usage
Middleware must be called as a function:cors(), not justcors.Step 2: Verify code
Code usesapp.use(cors);missing parentheses, causing error.Final Answer:
Missing parentheses when calling cors middleware -> Option AQuick Check:
Middleware must be called with () [OK]
- Passing middleware reference without ()
- Assuming app.get() lacks callback
- Forgetting to specify port in listen
helmet for security and express.json() for parsing JSON bodies in your Express app. Which code correctly adds both middlewares in the right order?Solution
Step 1: Understand middleware order importance
Security middleware like helmet should run before body parsing for best protection.Step 2: Check middleware calls
Both must be called as functions with parentheses; options B and C miss these, A has them but wrong order.Step 3: Confirm correct order and syntax
app.use(helmet()); app.use(express.json()); calls helmet first, then express.json(), both correctly called.Final Answer:
app.use(helmet()); app.use(express.json()); -> Option AQuick Check:
Helmet before json, both called [OK]
- Not calling middleware as functions
- Placing parsing before security middleware
- Mixing up middleware order
Ready to prove it?
Pass the test and this pattern moves unknown
