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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With