Node.js Express Routing

Route parameters, query strings and request bodies.

✍️ 2 exercises in this lesson
  • Methods: app.get, post, put, patch, delete
  • Route parameters: /users/:id → req.params.id
  • Query strings: /search?q=node → req.query.q
  • JSON bodies: add app.use(express.json()), then read req.body
  • express.Router() groups related routes into modules.

Key Takeaways: Node.js Express Routing

Mastering Node.js Express Routing helps you write clean and reliable Node.js code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Node.js Express Routing Code Example

Node.js
import express from "express";
const app = express();
app.use(express.json());

app.get("/users/:id", (req, res) => {
  res.json({ id: Number(req.params.id), name: "User " + req.params.id });
});

app.get("/search", (req, res) => {
  res.send(`You searched for: ${req.query.q}`);
});

app.post("/users", (req, res) => {
  const { name, email } = req.body;
  res.status(201).json({ id: 11, name, email });
});

app.listen(3000);
// GET /users/7         → {"id":7,"name":"User 7"}
// GET /search?q=node   → You searched for: node
Output
GET /users/7 → {"id":7,"name":"User 7"}
GET /search?q=node → You searched for: node
POST /users → 201 {"id":11,"name":"Sara","email":"sara@example.com"}
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

Node.js Express Routing Practice Exercises

Check what you learned in “Node.js Express Routing”.

/ 2 solved
Fill in the blank

Read the :id route parameter.

const id = req..id;
✓ Correct! Not quite. Try again.
Multiple choice

Which middleware lets Express read JSON request bodies?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services