Can we get the variables in query string in Node.js just like we get in $_GET
in PHP?
I know that in Node.js we can get the URL in request. Is there any method to get query string parameters?
Can we get the variables in query string in Node.js just like we get in I know that in Node.js we can get the URL in request. Is there any method to get query string parameters? |
||||
|
Yes you can:
|
|||||||||||||||||||||
|
Since you've mentioned Express.js in your tags, here is an Express-specific answer: use req.query. E.g.
|
|||||||||
|
In Express, use
That said, most of the time, you want to get the value of a parameter irrespective of its source. In that case, use:
The value of the parameter will be returned whether the variable was in the route parameters, query string, or the encoded request body. Side note- if you're aiming to get the intersection of all three types of request parameters (similar to PHP's |
|||||||||||||||||||||
|
for expressJs you want to do
|
|||||||||||||||||||||
|
I learned from above posts and decided to use this code throughout my site
then you can just call
where the url for get should be
|
|||
You should be able to do something like this:
|
||||
|
An old question, but since I've stumbled into it as top result when searching for something similar, here are my two cents. A small nodejs http server listening on port 9080, parsing GET or POST data and sending it back to the client as part of the response.
Save it as parse.js and run it on the console by entering "node parse.js" |
|||||
|
UPDATE 4 May 2014 Old answer preserved here: https://gist.github.com/stefek99/b10ed037d2a4a323d638 1) Install express: app.js
2) Run the app: 3) Visit in the browser:
(many things have changed since my answer and I believe it is worth keeping things up to date) |
||||
|
Whitequark responded nicely. But with the current versions of node and express it requires one more line. Make sure to add the 'require http' (second line). I've posted a fuller example here that shows how this call can work. Once running type the following in your browser
|
|||
|
It is so simple :- Example URL :
You can print all the values of query string by using :
Output
To print specific :
Output
Cheers!! |
||||
|