Node.js http get request parameters

I want to process an HTTP request as follows:

GET http://1.2.3.4/status?userID=1234 

But I canโ€™t extract the userID parameter from it. I use Express, but it does not help me. For example, when I write something like the following, this does not work:

 app.get('/status?userID=1234', function(req, res) { // ... }) 

I would like to be able to take the value 1234 for any local parameter, for example user=userID . How can i do this?

+4
source share
1 answer

You simply parse the request URL using your own module.

 var url = require('url'); app.get('/status', function(req, res) { var parts = url.parse(req.url, true); var query = parts.query; }) 

You will get something like this:

 query: { userID: '1234' } 

Edit:. Since you are using Express, query strings are automatically parsed.

 req.query.userID // returns 1234 
+12
source

All Articles