Express router.METHOD() Functions

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 3 Code Examples

What you’ll learn

  • How to define method-specific routes in router modules.
  • How mounted prefixes combine with router paths.
  • How to compose middleware chains on router.get() and router.post().
  • How to keep route modules clean and scalable.

Syntax

javascript
router.METHOD(path, handler)
router.METHOD(path, middleware1, middleware2, handler)
1

Define REST-style router methods

javascript
var router = require('express').Router();

router.get('/users', function (req, res) {
  res.send('List users');
});

router.post('/users', function (req, res) {
  res.status(201).send('Create user');
});
2

Use middleware chain in a route

javascript
function auth(req, res, next) {
  if (!req.user) return res.status(401).send('Unauthorized');
  next();
}

router.get('/account', auth, function (req, res) {
  res.json({ id: req.user.id });
});
3

Mount router with prefix

javascript
var app = require('express')();
var userRouter = require('./routes/users');

app.use('/api', userRouter);
// router path '/users/:id' becomes '/api/users/:id'

⚠️ Common pitfalls

  • Using the wrong HTTP method for operation semantics.
  • Forgetting that mount prefixes are part of the final route URL.
  • Not returning after early error responses in middleware chains.

❓ FAQ

It defines method-specific routes on an express.Router() instance, such as router.get() or router.post().
Functionality is similar, but router.METHOD() is used inside modular router files and then mounted with app.use().
Yes. You can pass middleware functions before the final route handler.
If mounted with app.use('/users', router), a router path '/:id' resolves to '/users/:id'.
Yes. It is a clean way to organize GET/POST/PUT/PATCH/DELETE handlers by feature.
Did you know?

router.METHOD() handlers keep routes modular while still following HTTP verb semantics like get, post, put, and delete.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

4 people found this page helpful