Node.js Express req.query未定義

[英]Node.js Express req.query is undefined


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

{{協議}}:// {{服務器}} / TEST1用戶名= 1

And I get the result:

我得到了結果:

{query: true}

{query:true}

Which shows req.query.username is indeed undefined

這表明req.query.username確實是未定義的

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

我錯過了什么?怎么沒有傳入查詢參數?

1 个解决方案

#1


2  

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

您顯示的代碼適用於節點v0.10.30並表達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=1 and it displays {"query":false}.

然后我導航到http:// localhost:8000 / test1?username = 1並顯示{“query”:false}。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/09/02/85082df28c8a31afa3e4a5884f9d49dd.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com