I would like to have different routes under an /api
path with different middleware handlers. Specifically different API routes should allow different ways of authenticating.
I thought I could nest different API routes under two different Router
s with different middleware:
const express = require("express");
const app = express();
const port = 4000;
const one = express.Router();
one.use((req, res, next) => {
console.log("one");
next();
});
one.get("/api/one", (req, res) => {
console.log("/api/one");
res.send("one");
});
// ---
const two = express.Router();
two.use((req, res, next) => {
console.log("two");
next();
});
two.get("/api/two", (req, res) => {
console.log("/api/two");
res.send("two");
});
// ---
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.use(one);
app.use(two);
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
I would expect that one
would get logged when navigating to /api/one
and two
gets logged when navigating to /api/two
, however both middlewares get run when navigating to /api/two
:
Example app listening on port 4000
one
/api/one
one
two
/api/two
Is there a way to achieve what I’m looking for without listing the different middleware for each individual API route?
2
Answers
The best I can think of here is to use different path prefixes, the router middleware will only get triggered for the specific route prefix.
Yes you can do this. Express allows multiple routers and you can mount them at different paths. A simple example would be:
RouterOne.cjs
RouterTwo.cjs
app.js