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?
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.
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