Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 299 Vote(s) - 3.59 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get GET (query string) variables in Express.js on Node.js?

#1
Can we get the variables in the query string in Node.js just like we get them in `$_GET` in PHP?

I know that in Node.js we can get the URL in the request. Is there a method to get the query string parameters?
Reply

#2
**UPDATE 4 May 2014**

Old answer preserved here:

---


1) Install express: ```npm install express```

**app.js**

var express = require('express');
var app = express();

app.get('/endpoint', function(request, response) {
var id = request.query.id;
response.end("I have received the ID: " + id);
});

app.listen(3000);
console.log("node express app started at

[To see links please register here]

");

2) Run the app: ```node app.js```


3) Visit in the browser: ```http://localhost:3000/endpoint?id=something```

> I have received the ID: something

---

*(many things have changed since my answer and I believe it is worth keeping things up to date)*
Reply

#3
You should be able to do something like this:

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

http.createServer(function(req,res){
var url_parts = url.parse(req.url, true);
var query = url_parts.query;

console.log(query); //{Object}

res.end("End")
})
Reply

#4
Since you've mentioned Express.js in your tags, here is an Express-specific answer: [use req.query](

[To see links please register here]

). E.g.

var express = require('express');
var app = express();

app.get('/', function(req, res){
res.send('id: ' + req.query.id);
});

app.listen(3000);
Reply

#5
For Express.js you want to do `req.params`:

app.get('/user/:id', function(req, res) {
res.send('user' + req.params.id);
});
Reply

#6
I learned from the other answers and decided to use this code throughout my site:

var query = require('url').parse(req.url,true).query;

Then you can just call

var id = query.id;
var option = query.option;

where the URL for get should be

/path/filename?id=123&option=456
Reply

#7
A small Node.js HTTP server listening on port 9080, parsing GET or POST data and sending it back to the client as part of the response is:

var sys = require('sys'),
url = require('url'),
http = require('http'),
qs = require('querystring');

var server = http.createServer(

function (request, response) {

if (request.method == 'POST') {
var body = '';
request.on('data', function (data) {
body += data;
});
request.on('end',function() {

var POST = qs.parse(body);
//console.log(POST);
response.writeHead( 200 );
response.write( JSON.stringify( POST ) );
response.end();
});
}
else if(request.method == 'GET') {

var url_parts = url.parse(request.url,true);
//console.log(url_parts.query);
response.writeHead( 200 );
response.write( JSON.stringify( url_parts.query ) );
response.end();
}
}
);

server.listen(9080);

Save it as `parse.js`, and run it on the console by entering "node parse.js".
Reply

#8
It is so simple:

**Example URL:**

[To see links please register here]

=$2a$08$jvGevXUOvYxKsiBt.PpMs.zgzD4C/wwTsvjzfUrqLrgS3zXJVfVRK

**You can print all the values of query string by using:**

console.log("All query strings: " + JSON.stringify(req.query));

**Output**

> All query strings : { "id":"3","activatekey":"$2a$08$jvGevXUOvYxKsiBt.PpMs.zgzD4C/wwTsvjz
fUrqLrgS3zXJVfVRK"}

**To print specific:**

console.log("activatekey: " + req.query.activatekey);

**Output**

> activatekey: $2a$08$jvGevXUOvYxKsiBt.PpMs.zgzD4C/wwTsvjzfUrqLrgS3zXJVfVRK
Reply

#9
Whitequark responded nicely. But with the current versions of Node.js and Express.js it requires one more line. Make sure to add the 'require http' (second line). I've posted a fuller example here that shows how this call can work. Once running, type `http://localhost:8080/?name=abel&fruit=apple` in your browser, and you will get a cool response based on the code.

var express = require('express');
var http = require('http');
var app = express();

app.configure(function(){
app.set('port', 8080);
});

app.get('/', function(req, res){
res.writeHead(200, {'content-type': 'text/plain'});
res.write('name: ' + req.query.name + '\n');
res.write('fruit: ' + req.query.fruit + '\n');
res.write('query: ' + req.query + '\n');
queryStuff = JSON.stringify(req.query);
res.end('That\'s all folks' + '\n' + queryStuff);
});

http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
})
Reply

#10
//get query&params in express

//etc. example.com/user/000000?sex=female

app.get('/user/:id', function(req, res) {

const query = req.query;// query = {sex:"female"}

const params = req.params; //params = {id:"000000"}

})
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through