0

I want to check for a consistent URL parameter in each request in my Express.JS app. The base of the URL is always the same:

/command/UUID

But some times it can be

/command/UUID/something/1

The following code works only in the first case. As soon as I add another slash at the end, this middleware won't be executed.

app.all('/*/:uuid', function(req, res, next) {

    console.log(req.params)

    next();

});

I would appreciate if someone could help me out solve this issue.

Best.

2 Answers 2

1

Something like this:

app.all('/:command/:uuid/(*?)', function(req, res, next) {

    console.log(req.params.command, req.params.uuid);

    next();

});
Sign up to request clarification or add additional context in comments.

5 Comments

Almost: it doesn't work if there is no / at the end.
Can you verify if you have enabled strict routing? It is disabled by default in Express though. app.set('strict routing', false);
It is set to false. And I can query the same URL with or without /.
that is not the solution that I'm looking for, I could also use the URL class from NodeJS, even better then splitting. But I would rather not do that. I would use what ExpressJS has to offer if possible.
0

What actually worked is the following code

app.set('strict routing', false );
app.all('/:cmd/:uuid*', function(req, res, next) {

    console.log(req.params)

    next();

});

And the result is as follow with this URL: /ip/165420/3123123/adasdsad

{ '0': '/3123123/adasdsad', cmd: 'ip', uuid: '165420' }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.