user2949712
user2949712

Reputation: 31

Retrieving Several GET Variables

I posted this before but for Express, which I am not using anymore.

I have tried looking on several websites but I still don't understand how I could retrieve several GET variables using Express. I would like to be able to ping a Node.JS Express server url using:

file_get_contents('http://127.0.0.1:5012/variable1/variable2/variable3/variable4');

or

file_get_contents('http://127.0.0.1:5012/?1=variable1&2=variable2&3=variable3&4=variable4');

Then I need to be able to use them inside the Node.JS Http script in the form of variable1, variable2, variable3 and variable4 and not all in a single string. An object to store them in would be fine.

So far, I have this code: app.listen(7777);

function handler (req, res) {
res.writeHead(200);
send(req.query.server.var1,req.query.server.var2,req.query.server.var3,req.query.server.var4,res);
}

But that only works for Express. I am now using the normal HTTP library for Node.JS Thanks :P

Upvotes: 0

Views: 48

Answers (1)

Vadim Baryshev
Vadim Baryshev

Reputation: 26199

You can use url API to parse request url.

var http = require('http');
var url = require('url');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  var query = url.parse(req.url, true).query;
  console.log(query);
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

Upvotes: 1

Related Questions