Express
Express app.use()
Authorization middleware decides whether a verified principal may perform an action. Its position determines which later handlers it protects. Authentication establishes identity; authorization evaluates permission. Put checks before protected side effects and enforce object access in services too. The example uses a fixed synthetic principal injected by a trusted test factory, not client headers or real authentication. Missing identity and insufficient permission are different cases. Trust proxy and forwarded headers require explicit deployment-specific review. Static example only; runtime NOT RUN. Source: https://expressjs.com/en/guide/using-middleware/.
Formal Syntax & Structure
nodejs syntaxnodejs
app.use(path, ...middleware)Working Example
Example Snippetnodejs
import express from 'express';export function createApp(testPrincipal) { const app = express(); app.use('/staff', (req,res,next) => { if (!testPrincipal) return res.sendStatus(401); if (testPrincipal.role !== 'staff') return res.sendStatus(403); next(); }); app.get('/staff/stock', (req,res) => res.json({ count: 4 })); return app;}