Express Routing Basics: Methods and Paths
Routing is the core of Express. Here is how methods and paths work, with examples.
Express Routing Basics: Methods and Paths
Routing in Express maps HTTP methods and URL paths to handlers. Here is how it works.
Basic Routes
app.get('/users', (req, res) => { /* list users */ });
app.post('/users', (req, res) => { /* create user */ });
app.put('/users/:id', (req, res) => { /* replace user */ });
app.patch('/users/:id', (req, res) => { /* update user */ });
app.delete('/users/:id', (req, res) => { /* delete user */ });
HTTP Methods
- GET: read data. Should be safe (no side effects) and idempotent.
- POST: create a new resource.
- PUT: replace a resource (full update).
- PATCH: partial update.
- DELETE: remove a resource.
URL Params
Use :name for params: /users/:id. Read them with req.params.id. Params are strings.
app.get('/users/:id', (req, res) => {
res.json({ id: req.params.id });
});
Query Strings
Anything after ? is in req.query: /users?search=foo&limit=10 gives req.query = { search: 'foo', limit: '10' }. Always strings; coerce when needed.
Wildcards
Use * for matching anything: app.get('/users/*', ...) matches /users/abc/def. Useful for catch-alls.
Multiple Handlers
A route can have multiple handlers, each calling next:
app.get('/posts/:id', authenticate, loadPost, (req, res) => {
res.json(req.post);
});
This is how you compose middlewares per route.
express.Router
For grouping routes by feature, use express.Router:
// routes/users.js
const router = express.Router();
router.get('/', listUsers);
router.post('/', createUser);
router.get('/:id', getUser);
module.exports = router;
// app.js
app.use('/users', require('./routes/users'));
This mounts all user routes under /users. Cleaner than putting every route in app.js.
The Takeaway
Express routing maps HTTP methods and paths to handlers. Methods: GET, POST, PUT, PATCH, DELETE. Use :name for URL params, ?key=value for query strings, and express.Router to group routes by feature. Multiple handlers per route enable per-route middleware composition.
You map HTTP methods and URL paths to handlers with app.get, app.post, etc. Use :name for URL params (req.params), ?key=value for query strings (req.query), and express.Router to group routes by feature.
GET for reading (safe, idempotent), POST for creating, PUT for full replace, PATCH for partial update, DELETE for removing. Pick by semantics, not by habit.
Define a route with :name: /users/:id. Read it with req.params.id. Params are always strings. Convert to ObjectId or number when needed.
For grouping routes by feature. You create a router in routes/users.js, add routes to it, and mount it in app.js with app.use('/users', usersRouter). Cleaner than putting every route in app.js.
Yes. Each handler calls next() to pass control to the next one. This lets you compose middlewares per route: authenticate, loadPost, then the handler. Very useful for protected routes.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

