Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET querystring in node.js

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?

like image 526
user1164541 Avatar asked Jul 22 '26 09:07

user1164541


1 Answers

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.

like image 176
hexacyanide Avatar answered Jul 23 '26 23:07

hexacyanide