29

Scenario: We developer are trying to replace a web service (written in C#.Net) with Node.JS Restful API.

Issue: Now we need to handle the incoming request as is (we don't have control over it). So the following is the format of the incoming URL:

http://www.website.com/Service.aspx?UID=Trans001&FacebookID=ae67ea324&GetDetailType=FULL

I am able to handle the URL like:

http://www.website.com/service/Trans001/ae67ea324/FULL

I can parse/read the parameter from the above URL

Code:

var server = require('restify').createServer();
function respond(req, res, next) {
    console.log("req.params.UID:" + req.params.UID);
    console.log("req.params.FacebookID:" + req.params.FacebookID);
    console.log("req.params.GetDetailType" + req.params.GetDetailType);
}
server.get('/service/:UID/:FacebookID/:GetDetailType', respond);
server.listen(8080, function () {
    console.log('%s listening at %s', server.name, server.url);
});

Question: How can I read the multiple parameters from the URL which is formatted like http://www.website.com/Service.aspx?UID=Trans001&FacebookID=ae67ea324

Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164

3 Answers3

75

You just need to load the query parser plugin like so;

server.use(restify.plugins.queryParser());
Paul Weber
  • 6,518
  • 3
  • 43
  • 52
Simon
  • 37,815
  • 2
  • 34
  • 27
5

Restify 5 (2017) answer:

As of restify 5 you can now setup the query parser like this: server.use(restify.plugins.queryParser());.

If you use this plugin you can access the parsed params in req.query.

For additional options and information, take a look into the restify documentation: http://restify.com/docs/plugins-api/#queryparser

kentor
  • 16,553
  • 20
  • 86
  • 144
2

Simon's answer is no longer valid as restify's queryParser has been moved to the restify-plugins package. The updated solution is

server.use(require('restify-plugins').queryParser());
therightstuff
  • 833
  • 1
  • 16
  • 21
  • 6
    And now `restify-plugins` is deprecated... the updated solution is `server.use(restify.plugins.queryParser());` – Nafis Zaman Aug 17 '17 at 21:35