How to get GET (query string) variables in Express.js on Node.js?

前端 未结 26 3477
庸人自扰
庸人自扰 2020-11-21 23:53

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.

相关标签:
26条回答
  • 2020-11-22 00:00

    In Express, we can simply use req.query.<name>. It's works same as that of $_GET['name'] in PHP.

    0 讨论(0)
  • 2020-11-22 00:02

    In Express, use req.query.

    req.params only gets the route parameters, not the query string parameters. See the express or sails documentation:

    (req.params) Checks route params, ex: /user/:id

    (req.query) Checks query string params, ex: ?id=12 Checks urlencoded body params

    (req.body), ex: id=12 To utilize urlencoded request bodies, req.body should be an object. This can be done by using the _express.bodyParser middleware.

    That said, most of the time, you want to get the value of a parameter irrespective of its source. In that case, use req.param('foo').

    The value of the parameter will be returned whether the variable was in the route parameters, query string, or the encoded request body.

    Side note- if you're aiming to get the intersection of all three types of request parameters (similar to PHP's $_REQUEST), you just need to merge the parameters together-- here's how I set it up in Sails. Keep in mind that the path/route parameters object (req.params) has array properties, so order matters (although this may change in Express 4)

    0 讨论(0)
  • 2020-11-22 00:02

    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);
    });
    
    0 讨论(0)
  • 2020-11-22 00:02

    I am using MEANJS 0.6.0 with express@4.16, 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
      ...
    
    0 讨论(0)
  • 2020-11-22 00:03

    UPDATE 4 May 2014

    Old answer preserved here: https://gist.github.com/stefek99/b10ed037d2a4a323d638


    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 http://localhost:3000");
    

    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)

    0 讨论(0)
  • 2020-11-22 00:04

    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:

    // 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)

    0 讨论(0)
提交回复
热议问题