How to parse/read multiple parameters with restify framework for Node.JS

26,454

Solution 1

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

server.use(restify.plugins.queryParser());

Solution 2

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

Solution 3

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());
Share:
26,454
Amol M Kulkarni
Author by

Amol M Kulkarni

Fell in ♥ with Codes... Especially JS ;-) Currently Working on broader web platform (targeting all possible devices and browsers)You'll find me contributing, authoring many projects across the internet. I also ♥ helping others in solving technical problem & I believe this is a way to stay up-to-date and give something back to the community from where I have learnt and still learning :) Worked on: ADO.NET, AJAX, ASP.NET, C, C#, C++, Corel draw, CouchDB, CouchBase, Crystal Reports, CSS, DHTML, Dreamweaver, EJS, Express Framework Node.js, Go-lang, HTML, Infragistics, Jade, Java, Java Applets, JavaScript, jQuery, JSP, Membase, Memcache, Microsoft SQL Server, MongoDB with Python and Node.js, MongoDB(DBA), MongoDB (4.2-Basics), MongoDB New Features and Tools in 4.2, MongoDB Performance Tuning, MS-DOS, Netbeans, No SQL, Node.js, Oracle, OAuth, PageMaker, PhotoShop, Servlets, Socket.io, SwishMax, Tally, VBA, VBScript, Visual Basic, Visual InterDev, Windows Mobile C#, XHTML, XML, XQuery, XSLT, Python, PHP, Kafka, Storm, 0MQ (ZMQ), Redis, Hadoop Favorite Quotation: "Only He Who Can See The Invisible Can Do The Impossible." profile for Amol M Kulkarni on Stack Exchange, a network of free, community-driven Q&A sites http://stackexchange.com/users/flair/2185406.png

Updated on July 09, 2022

Comments

  • Amol M Kulkarni
    Amol M Kulkarni almost 2 years

    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