Skip to content
Learn Netverks

Lesson

Step 20/36 56% through track

routing-middleware

Routing and middleware

Last reviewed May 28, 2026 Content v20260528
Track mode
nodejs_server
Means
Node sandbox
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Routing and middleware: the syntax, APIs, and habits you need before advancing in Node.js.

Teams ship Routing and middleware on every Node.js codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Routing and middleware in contexts like: REST/GraphQL APIs, BFF layers, CLIs, webhooks, and real-time services (with WebSockets).

Run JavaScript on the Node runner when configured—never mix arbitrary shell commands in lessons.

When you can explain the previous lesson's ideas without copying starter code.

Routing maps HTTP method + path to handlers. Middleware runs in order—logging, auth, body parsing—each calls next() or ends the response.

Express middleware chain

app.use((req, res, next) => {
  console.log(req.method, req.url);
  next();
});

app.use(express.json());

app.get('/api/health', (req, res) => {
  res.json({ ok: true });
});

Order matters

Auth middleware before protected routes; error handler last with four args (err, req, res, next). Missing next() hangs requests.

Route parameters

app.get('/users/:id', (req, res) => {
  res.json({ id: req.params.id });
});

Important interview questions and answers

  1. Q: What if middleware never calls next or res.end?
    A: Client hangs until timeout—common bug when forgetting to return after sending response.
  2. Q: app.use vs app.get?
    A: use matches all methods/prefix paths; get matches GET on exact route patterns.

Self-check

  1. What does next() do?
  2. Why put error middleware last?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Router mount path?
  • next() forgotten bug?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump