Reputation: 3616
I'm attempting to use the Express 4 Router to use a different router based on the path. I want all /api/v2/
routes handled by version2
, and every other route handled by version1
. The below setup serves me all the version1
routes correctly, but seems to ignore version2
as I get a 'Cannot GET...' message each time I test those endpoints.
routes.js:
var version1 = require('./routes/vers1');
var version2 = require('./routes/vers2');
module.exports = function(app) {
app.all('/api/v2/*', version2);
app.all('/*', version1);
};
Upvotes: 1
Views: 2973
Reputation: 191809
Method routes (.get
, .post
, and of course .all
) are terminal. This is why you can use wildcards with them as well. .use
is not terminal and doesn't allow wildcards -- it acts as a prefix. This is an implementation choice of express. Use .use
without wildcards.
.use
does not set the layer route/api/v2
that you set in .use
will be stripped).The fact that app.use("/*", version1)
works is purely incidental. This will match any route and fall through to version1[method](path)
. Since there is no prefix to strip, if the request route matches path
, express will consider this a match and serve that route.
Use .use
.
Upvotes: 2
Reputation: 395
You want to use .use not .all
.all is for middleware like authentication
Upvotes: 2