Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Express.js · Const

strict routing

express.Router({ strict: true }) makes trailing slashes significant when matching routes. By default, /foo and /foo/ are treated as the same path.

How it works

Pass strict: true to express.Router() to make trailing slashes significant for routes managed by that router. With the default setting, a path such as /events also matches /events/; strict routing separates those two forms.

Syntax

express.Router([options])

Parameters

NameTypeDescription
optionsObjectOptional router configuration object; its strict Boolean controls whether trailing slashes distinguish paths.

Returns

Router

Key points

  • The option accepts a Boolean and defaults to false.
  • Setting the option to true enables strict route matching.
  • Without strict routing, a route and its trailing-slash variant are equivalent.

Common mistakes

  • Strict routing is off unless explicitly enabled, so relying on the default can unintentionally accept both trailing-slash forms.
  • The option controls trailing-slash matching, not general path matching or HTTP-method behavior.

Example: Strict router paths

const express = require('express');

const app = express();
const router = express.Router({ strict: true });

router.get('/events', (req, res) => res.send('events'));
app.use('/calendar', router);

app.listen(3000);

Creates a router where /events and /events/ are distinct paths, then mounts it under /calendar.

When to use it

Enforce URL shape

An API needs canonical URL forms and should reject requests that add or omit a trailing slash.

Configure a sub-router

A mounted feature router must use stricter path matching without changing unrelated routers.

Distinguish trailing-slash routes

Two endpoints intentionally differ only by whether the path ends in a slash.

Interview questions

QHow do you enable strict routing for an Express router?

Set the router option to strict: true, for example express.Router({ strict: true }). This makes /foo and /foo/ different paths.

Follow-up: What happens when the option is omitted? The router defaults to non-strict behavior, treating both forms as equivalent.

QWhat is the default value and behavior of the router's strict option?

The option is Boolean and defaults to false. Therefore, a router normally treats /foo and /foo/ as the same route.

Follow-up: How would you make only one router strict? Create that router with strict: true; other routers can retain their own options.

QHow would you apply strict routing to a mounted sub-router?

Use express.Router({ strict: true }), add routes to the returned router, and mount it with app.use(). The router remains middleware-compatible while applying its route-matching policy.

Follow-up: Can strict routing be combined with case sensitivity? Yes; pass both options, such as { caseSensitive: true, strict: true }.

  • express.Router()
  • caseSensitive
  • router.get()
  • app.use()

Source: Express Object · Express.js 4.x. Checked 2026-10-15.