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

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

    It is so simple:

    Example URL:

    http://stackoverflow.com:3000/activate_accountid=3&activatekey=$2a$08$jvGevXUOvYxKsiBt.PpMs.zgzD4C/wwTsvjzfUrqLrgS3zXJVfVRK
    

    You can print all the values of query string by using:

    console.log("All query strings: " + JSON.stringify(req.query));
    

    Output

    All query strings : { "id":"3","activatekey":"$2a$08$jvGevXUOvYxKsiBt.PpMs.zgzD4C/wwTsvjz fUrqLrgS3zXJVfVRK"}

    To print specific:

    console.log("activatekey: " + req.query.activatekey);
    

    Output

    activatekey: $2a$08$jvGevXUOvYxKsiBt.PpMs.zgzD4C/wwTsvjzfUrqLrgS3zXJVfVRK

提交回复
热议问题