Using 'querystring.parse' built-in module's method in Node.JS to read/parse parameters

13,471
  1. I think you need req.url rather than req._url.

  2. req.url is a string, if you want a URI instance use require('url').parse(req.url)

  3. So, you should finally have:

    var ParamsWithValue = querystring.parse(require('url').parse(req.url).query);

Edit: I corrected a typo in point 1, the last req.url -> req._url

Share:
13,471
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 June 05, 2022

Comments

  • Amol M Kulkarni
    Amol M Kulkarni almost 2 years

    Scenario: Consider the following code:

    var querystring = require('querystring');
    var ParamsWithValue = querystring.parse(req._url.query);
    

    Then I am able to read any query string's value.
    E.g: If requested string is http://www.website.com/Service.aspx?UID=Trans001&FacebookID=ae67ea324
    I can get the values of query string with codes ParamsWithValue.UID & ParamsWithValue.FacebookID respectively.

    Issue: I am able to get the values of any number of parameters passed in the same way described above. But for second time onwards I am getting the following error in response on browser.

    Error:

    {"code":"InternalError","message":"Cannot read property 'query' of undefined"}
    

    Question: What is wrong in the approach to read the query string from the URL.

    Note: I don't want to use any frameworks to parse it. I am trying to depend on built-in modules only.


    Update: It responds correctly when the value of any of the parameter is changed. But if the same values requested again from even different browser it throws same error.
  • Amol M Kulkarni
    Amol M Kulkarni about 11 years
    I am not clear with what you tried to convey! 1.I think you need req.url rather than req.url.
  • major-mann
    major-mann about 11 years
    Glad you got it working, I made a mistake.. it was supposed to be req._url... I will edit the answer :)