64

Let's say I have get route like this:

app.get('/documents/format/type', function (req, res) {
   var format = req.params.format,
       type = req.params.type;
});

So if I make request like

http://localhost:3000/documents/json/mini

in my format and type variables will be 'json' and 'mini' respectively, but if I make request like

http://localhost:3000/documents/mini/json

not. So my question is: how can I get the same variables in different order?

1
  • 1
    You don't documents/mini/json is format == mini and type == json. URL's are not unordered bags of parameters Commented Dec 14, 2011 at 15:38

2 Answers 2

173

Your route isn't ok, it should be like this (with ':')

app.get('/documents/:format/:type', function (req, res) {
   var format = req.params.format,
       type = req.params.type;
});

Also you cannot interchange parameter order unfortunately. For more information on req.params (and req.query) check out the api reference here.

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

1 Comment

//var sanitizer = require('sanitizer'); var format = sanitizer.escape(req.params. format); You really should sanitize the result. Or else your website has a HUGE vulnerability
58

For Query parameters like example.com/test?format=json&type=mini format, then you can easily receive it via req.query.<param>

app.get('/test', function(req, res){
  var format = req.query.format,
      type = req.query.type;
});

3 Comments

Thanks. Your answer is also useful for me!
This made it so much easier to deal with a parameter that is a URL itself.
+1 This answers the question in the title, which I came here to find (the actual question has nothing to do with get params).

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.