Reputation: 61
I need to edit a node.js app GET a querystring, without (i) using Express or any other modules and (ii) creating a server in addition to the one which already exists.
I want to pass ?id=helloworld into the variable id.
How would I do this?
Upvotes: 3
Views: 8415
Reputation: 91799
You can use the native querystring
module to parse query strings.
var http = require('http');
var qs = require('querystring');
http.createServer(function (req, res) {
var str = req.url.split('?')[1];
qs.parse(str);
});
Parsing query strings will return results in an object:
qs.parse('foo=bar&baz=qux&baz=quux&corge')
// returns
{ foo: 'bar', baz: ['qux', 'quux'], corge: '' }
You can also find the source of the module here.
Upvotes: 8