javascript Node.js Express req.query 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25615532/
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-10-28 04:44:08  来源:igfitidea点击:

Node.js Express req.query is undefined

javascriptnode.jsexpress

提问by Ron Harlev

I'm running the following code on Node.js with Express 4.7.2

我正在使用 Express 4.7.2 在 Node.js 上运行以下代码

express.get('/test1',function(req, res) {
  var ttt = false;
  if (req.query.username === undefined) ttt = true;
  res.json({query: ttt});
});

I call the URL:

我调用 URL:

{{protocol}}://{{server}}/test1?username=1

{{protocol}}://{{server}}/test1?username=1

And I get the result:

我得到了结果:

{query: true}

{查询:真}

Which shows req.query.usernameis indeed undefined

哪个节目req.query.username确实是undefined

What am I missing? How come the query param is not passed in?

我错过了什么?为什么没有传入查询参数?

回答by mscdex

The code you've shown works fine for me with node v0.10.30 and express 4.8.7:

您显示的代码对我来说适用于 node v0.10.30 和 express 4.8.7:

var app = require('express')();

app.get('/test1',function(req, res) {
  var ttt = false;
  if (req.query.username === undefined) ttt = true;
  res.json({query: ttt});
});

app.listen(8000);

I then navigate to http://localhost:8000/test1?username=1and it displays {"query":false}.

然后我导航到http://localhost:8000/test1?username=1并显示{"query":false}.