Code Snippet - How to Access Query String at Node.js
2014/05/021 min read
bookmark this
Table of Contents
Introduction
This post shows a quick code snippet for accessing query string parameters in Node.js.
Accessing the Query String
If you want to get the query string like the following:
http://localhost:9000/api/myapi?id=1
In your Node.js code, you can write the following to get the id's value in the function you defined for your API:
var url = require('url');
var url_parts = url.parse(req.url, true);
console.info(url.parts.querystring.id);
Conclusion
Using the built-in url module in Node.js, you can easily parse and access query string parameters from incoming requests.