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

前端 未结 26 3483
庸人自扰
庸人自扰 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:21

    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
    });
    

提交回复
热议问题