Javascript express js中的请求参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14053863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:35:21  来源:igfitidea点击:

Request Parameter in express js

javascriptnode.jsexpress

提问by tk120404

I am using express.js and I am trying to fetch the request parameter using the following code.

我正在使用 express.js 并尝试使用以下代码获取请求参数。

app.configure(function () {
  app.use(express.static(__dirname + '/public'));
});

app.get('/', function (req, res) {
  console.log(req.params[0]);
  console.log(req.params.id);
  res.render('/public/index.html');
});

My url looks like this.

我的网址看起来像这样。

http://localhost:8080/?id=34.

I am not getting the request parameter. I tried all possibilities.

我没有得到请求参数。我尝试了所有的可能性。

回答by Dominic Barnes

You need to reference req.query

你需要参考 req.query

req.paramsis for params embedded in the URL path.

req.params用于嵌入在 URL 路径中的参数。

回答by Abhijeet

You can also use

你也可以使用

req.param('id')

req.param('id')

It will fetch path,body and query params for you.

它将为您获取路径、正文和查询参数。