TGViewer
Web Development Web Development @webdevcoursefree · 79.7K subscribers
Post #3316 8.45K
✅ Backend Basics Interview Questions – Part 3 (Express.js Middleware) 🚀🧠

📍 1. What is Middleware in Express.js?
A: Middleware are functions that execute during the request-response cycle. They can modify req/res, execute code, or end the request.

📍 2. Types of Middleware
⦁ Application-level: Applied to all routes or specific routes (using app.use())
⦁ Router-level: Applied to router instances
⦁ Built-in: e.g., express.json(), express.static()
⦁ Third-party: e.g., cors, morgan, helmet

📍 3. Example – Logging Middleware
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`);
next(); // Pass to next middleware/route
});


📍 4. Built-in Middleware
app.use(express.json()); // Parses JSON body
app.use(express.urlencoded({ extended: true })); // Parses URL-encoded body
app.use(express.static('public')); // Serves static files


📍 5. Router-level Middleware
const router = express.Router();
router.use((req, res, next) => {
console.log('Router Middleware Active');
next();
});
app.use('/api', router);


📍 6. Error-handling Middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});


📍 7. Chaining Middleware
app.get('/profile', authMiddleware, logMiddleware, (req, res) => {
res.send('User Profile');
});


💡 Pro Tip: Middleware order matters—always place error-handling middleware last.

💬 Tap ❤️ for more!
  • ❤ 16
More from @webdevcoursefree
  1. Oct 16, 2026𝗗𝗮𝘁𝗮 𝘀𝗰𝗶𝗲𝗻𝗰𝗲 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗶𝗼𝗻 𝗪𝗶𝘁𝗵 𝗔𝗜😍 Future Ready Data Scien…
  2. Oct 16, 2026🌐 Web Development Roadmap — Part 18 HTML Lists Lists are used whenever information needs…
  3. Oct 14, 2026🌐 Web Development Roadmap — Part 17 HTML Images — <img> Tag Images make webpages more vis…
  4. Oct 13, 2026𝗧𝗢𝗣 𝗙𝗥𝗘𝗘 𝗖𝗢𝗨𝗥𝗦𝗘𝗦 𝗧𝗢 𝗕𝗢𝗢𝗦𝗧 𝗬𝗢𝗨𝗥 𝗦𝗞𝗜𝗟𝗟𝗦 𝗜𝗡 𝟮𝟬𝟮𝟲! Explor…
  5. Oct 12, 2026For example: Go to Profile → <a> Submit Form → <button> Open Article → <a> Delete Account…
  6. Oct 12, 2026🌐 Web Development Roadmap — Part 16 HTML Links — Tag Links are one of the most important…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →