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

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

    You can use

    request.query.<varible-name>;
    
    0 讨论(0)
  • 2020-11-22 00:17

    why not mixed with server code

    e.g . php

    <script>
    var ip=<?php echo($_SERVER['REMOTE_ADDR']);?>
    
    </script>
    
    0 讨论(0)
  • 2020-11-22 00:20

    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".

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

    Since you've mentioned Express.js in your tags, here is an Express-specific answer: use req.query. E.g.

    var express = require('express');
    var app = express();
    
    app.get('/', function(req, res){
      res.send('id: ' + req.query.id);
    });
    
    app.listen(3000);
    
    0 讨论(0)
  • 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
    });
    
    0 讨论(0)
  • 2020-11-22 00:22

    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")
    })
    
    0 讨论(0)
提交回复
热议问题