Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js http get request parameters [duplicate]

I want to handle an HTTP request like this:

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

But I can't extract the parameter userID from it. I am using Express, but it's not helping me. For example, when I write something like the following, it doesn't work:

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

I would like to have possibility to take value 1234 for any local parameter, for example, user=userID. How can I do this?

like image 736
Alex Cebotari Avatar asked Sep 26 '13 13:09

Alex Cebotari


1 Answers

You just parse the request URL with the native 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're using Express, query strings are automatically parsed.

req.query.userID
// returns 1234
like image 168
hexacyanide Avatar answered Oct 03 '22 12:10

hexacyanide