JoelParke
JoelParke

Reputation: 2736

In express I have a route that doesn't match and I want to know why?

When I send:

PATCH /55148df6935c4bac084b30b2/token/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NTE0OGRmNjkzNWM0YmFjMDg0YjMwYjIiLCJpYXQiOjE0MzAyNTE2OTg5NDIsImV4cCI6MTQzMDI1MTkzODk0Mn0.n8Itqpaf7hjyq23ke977S0oamqXSwEF9iFR62Mlo9Sw

I always get back 404? Why? What am I missing?

The route is

router.patch('/:id/token/(.+)/', ...)

Clearly I am missing some assumption. Is the string too long? Is the '.' in the string causing issues? Can you help?

Upvotes: 1

Views: 112

Answers (1)

Andrew Lavers
Andrew Lavers

Reputation: 8141

Express doesn't accept raw regex patterns in the path like this. You need to attach them to a named route parameter. You could specify a regex like this: :token([\\w\.]+), but the default :token should already match your test string. For example, this is probably what you're looking for:

router.patch('/:id/token/:token', function(req, res) {
    console.log('My ID:', req.params.id);
    console.log('My token:', req.params.token);
})

But I would also suggest sending PATCH data in the body of the request and not in the request path. You can send them as url encoded form parameters (eg token=1234abcd) or as JSON for example. This is more "canonical" and the other advantage is that you don't have potentially sensitive information getting saved in the browser history and HTTP logs.

Upvotes: 1

Related Questions