1

I am currently trying to figure out how can I re-design an incoming url request in node.js and read the parameters from there.I am using restify.

In my router.js :

server.get('/myDomainName/myService/:location/:userId',myFunction);

I am getting the "location" and "userId" as parameter from here and further processing.

This is working perfectly. But I need to redesign the URL like ,

/myDomainName/myService?location={someLocation}&userId={someID}.

So, I have designed the URL like this :

/myDomainName/myService?location=:x&userId=:y

But when I am trying to read the value of x and y (console.log(request.params.x)) , they are undefined. I need to use that x and y value for further processing.

What I am doing wrong here for URL design ? How can I implement this ?

Thanks in advance.

  • possible duplicate of [How to parse/read multiple parameters with restify framework for Node.JS](http://stackoverflow.com/questions/15830448/how-to-parse-read-multiple-parameters-with-restify-framework-for-node-js) – mikaelb Dec 19 '14 at 07:16

2 Answers2

2

Yo can set the url like: /myDomainName/myService and get the params from req.query.location and req.query.userId

This might help you: http://expressjs.com/api.html#req.query

naoxink
  • 595
  • 1
  • 12
  • 19
  • Thanks for the reply . Understood the difference between req.query and req.params . Unfortunately , still it is showing Undefined. Now What I am doing : server.get('/myDomainName/myService',myFunction); Then when I am hitting /myDomainName/myService?localtion=bd ,in browser. console.log(request.query.location); --> Undefined :( – Nazar-E-Bukhari Dec 19 '14 at 07:45
  • You can print `req.query` and see what is the problem. It should work :/ – naoxink Dec 19 '14 at 07:46
  • You have set `server.use(restify.queryParser());` ? – naoxink Dec 19 '14 at 07:52
  • Oh !!! That was the problem. No , I did not set that . Great Help . Now it's working. Many Thanks :) – Nazar-E-Bukhari Dec 19 '14 at 07:55
0

Query string is not included in the route path, its available in req.query. Just ignore query string when designing the route path.

zuo
  • 1,061
  • 1
  • 12
  • 27