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

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

提交回复
热议问题