ThinkFloyd
ThinkFloyd

Reputation: 5021

Express sometimes picking wrong route

I am having a NodeJS based REST service exposed using Express (4.0.0) where I have two different routes like this:

router.get('/buckets/:bucketId/entities/bulk', getEntitiesInBulk);

router.get('/buckets/:bucketId/entities/:key', getEntityByKey);

When I send a request like this:

http://<server:port>/buckets/responses/entities/k3

The request is being handled by getEntityByKey() which I have defined there, but strangely when I bombard it with many requests it sometimes get handled by getEntitiesInBulk() and gets some error in response which is only thrown by getEntitiesInBulk().

I am totally confused about how is this possible.

Upvotes: 1

Views: 1238

Answers (1)

Brasilikum
Brasilikum

Reputation: 121

Express is confused because your routes are not unique. "bulk" will sometimes be used as a :key in the first route. Simply change the signature a bit, like

router.get('/buckets/:bucketId/entities/bulk', getEntitiesInBulk);
router.get('/buckets/:bucketId/entity/:key', getEntityByKey);

Upvotes: 2

Related Questions