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?

#11
You can use

request.query.<varible-name>;
Reply

#12
you can use url module to collect parameters by using url.parse

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

In expressjs it's done by,

var id = req.query.id;
Eg:

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

app.get('/login', function (req, res, next) {
console.log(req.query);
console.log(req.query.id); //Give parameter id
});
Reply

#13
I am using **MEANJS 0.6.0** with [email protected], it's good

**Client:**

Controller:

var input = { keyword: vm.keyword };
ProductAPi.getOrder(input)

services:

this.getOrder = function (input) {return $http.get('/api/order', { params: input });};

**Server**

routes

app.route('/api/order').get(products.order);

controller

exports.order = function (req, res) {
var keyword = req.query.keyword
...


Reply

#14
In `express.js` you can get it pretty easy, all you need to do in your controller function is:

app.get('/', (req, res, next) => {
const {id} = req.query;
// rest of your code here...
})

And that's all, assuming you are using es6 syntax.

PD. `{id}` stands for `Object destructuring`, a new es6 feature.
Reply

#15
You can use with express ^4.15.4:

var express = require('express'),
router = express.Router();
router.get('/', function (req, res, next) {
console.log(req.query);
});

Hope this helps.
Reply

#16
If you are using **ES6** and **Express**, try this `destructuring` approach:

const {id, since, fields, anotherField} = request.query;


In context:

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

app.get('/', function(req, res){
const {id, since, fields, anotherField} = req.query;
});

app.listen(3000);

You can use default values with `destructuring` too:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// sample request for testing
const req = {
query: {
id: '123',
fields: ['a', 'b', 'c']
}
}

const {
id,
since = new Date().toString(),
fields = ['x'],
anotherField = 'default'
} = req.query;

console.log(id, since, fields, anotherField)

<!-- end snippet -->

Reply

#17
So, there are two ways in which this "id" can be received:
1) using params: the code params will look something like :
Say we have an array,
```
const courses = [{
id: 1,
name: 'Mathematics'
},
{
id: 2,
name: 'History'
}
];
```
Then for params we can do something like:
```
app.get('/api/posts/:id',(req,res)=>{
const course = courses.find(o=>o.id == (req.params.id))
res.send(course);
});
````
2) Another method is to use query parameters.
so the url will look something like ".....\api\xyz?id=1" where "?id=1" is the query part. In this case we can do something like:
```
app.get('/api/posts',(req,res)=>{
const course = courses.find(o=>o.id == (req.query.id))
res.send(course);
});
```
Reply

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

You can use this or you can try body-parser for parsing special element from the request parameters.
Reply

#19
There are 2 ways to pass parameters via GET method<br/><br/>
Method 1 :
The MVC approach where you pass the parameters like /routename/:paramname<br/>
In this case you can use req.params.paramname to get the parameter value For Example refer below code where I am expecting Id as a param<br/>
link could be like :

[To see links please register here]


var express = require('express');
var app = express();
app.get("items/:id", function(req, res) {
var id = req.params.id;
//further operations to perform
});
app.listen(3000);

Method 2 :
General Approach : Passing variables as query string using '?' operator <br/>
For Example refer below code where I am expecting Id as a query parameter<br/>
link could be like :

[To see links please register here]


var express = require('express');
var app = express();
app.get("/items", function(req, res) {
var id = req.query.id;
//further operations to perform
});
app.listen(3000);
Reply

#20
If you ever need to send `GET` request to an `IP` as well as a `Domain` (Other answers did not mention you can specify a `port` variable), you can make use of this function:

function getCode(host, port, path, queryString) {
console.log("(" + host + ":" + port + path + ")" + "Running httpHelper.getCode()")

// Construct url and query string
const requestUrl = url.parse(url.format({
protocol: 'http',
hostname: host,
pathname: path,
port: port,
query: queryString
}));

console.log("(" + host + path + ")" + "Sending GET request")
// Send request
console.log(url.format(requestUrl))
http.get(url.format(requestUrl), (resp) => {
let data = '';

// A chunk of data has been received.
resp.on('data', (chunk) => {
console.log("GET chunk: " + chunk);
data += chunk;
});

// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log("GET end of response: " + data);
});

}).on("error", (err) => {
console.log("GET Error: " + err);
});
}

Don't miss requiring modules at the top of your file:

http = require("http");
url = require('url')
Also bare in mind that you may use `https` module for communicating over secured domains and ssl. so these two lines would change:

https = require("https");
...
https.get(url.format(requestUrl), (resp) => { ......
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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