Express req.query始终为空 [英] Express req.query always empty
问题描述
我正在使用像这样的快速路由,我希望我的url最初包含查询字符串.
I was using express route like this and I want my urls to contain query strings initially.
app.get('/', function(req, res){
res.render('index', {});
});
app.get('/us01', function(req, res){
console.log('query: '+JSON.stringify(req.query));
res.render('templates/us01', {});
});
app.get('/benchmark', function(req, res){
res.render('templates/benchmark', {});
});
但是,无论我在/us01之后附加什么查询字符串,我都不会打印查询字符串.例如,"localhost:9200/us01?a = 1" req.query应该让我{a:1},对吗?这是平常的事吗?我在这里想念什么?
However, I never get my query strings printed no matter what query strings I append after /us01. For example, "localhost:9200/us01?a=1" req.query should get me {a:1}, correct? Is this a common thing? What am I missing here?
我的app.js
"use strict";
var express = require('express');
var expApp = express();
var http = require('http').Server(expApp);
var path = require('path');
var bodyParser = require('body-parser');
// all environments
expApp.set('port', process.env.PORT || 5555);
expApp.set('views', __dirname + '/views');
expApp.set('view engine', 'ejs');
expApp.use(bodyParser.urlencoded({ extended: true }));
expApp.use(bodyParser.json());
expApp.use(express.static(path.join(__dirname, 'public')));
//----------------ROUTES--------------------------//
require("./routes/route.js")(expApp);
http.listen(expApp.get('port'), function(){
console.log('Node-Server listening on port ' + expApp.get('port'));
});
我的indexController.js具有:
My indexController.js has :
$stateProvider
.state('us01', {
url: '/us01',
templateUrl: '/us01'
}).state('benchmark', {
url: '/benchmark',
templateUrl: '/benchmark'
})....
推荐答案
这个简单的代码:
const express = require('express');
const app = express();
app.get('/us01', function(req, res) {
console.log(req.query);
res.send("ok");
});
app.listen(80);
然后,由http://localhost/us01?a=1
访问的结果将在控制台中产生以下输出:
Then, accessed by http://localhost/us01?a=1
produces this output in the console:
{ a: '1' }
或者,如果我使用:
console.log('query: ' + JSON.stringify(req.query));
然后,我在控制台中看到了这一点:
Then, I see this in the console:
query: {"a":"1"}
因此,很明显,您的代码中有其他错误.
So, clearly something else is wrong in your code.
"localhost:9200/us01?a = 1" req.query应该让我{a:1},对吗?
"localhost:9200/us01?a=1" req.query should get me {a:1}, correct?
如果您显示的代码正在本地主机上的端口9200上运行,它将为您带来query: {"a":"1"}
.
It should get you query: {"a":"1"}
if the code you show is running on port 9200 on localhost.
这是平常的事吗?
Is this a common thing?
不.您所显示的代码之外的其他东西都被破坏了,因为仅那部分代码并没有什么问题.
No. Something other than the code you show is busted because there's nothing wrong with just that bit of code.
我在这里想念什么?
What am I missing here?
要检查的事情:
- 当您击中任何预期路线时,您是否在控制台中获得任何输出?
- 您能证明您的服务器正在运行并且浏览器正在运行您的路由处理程序吗?
- 如果您只是执行
console.log(req.query)
,您会得到什么输出? - 您完全确定已杀死所有以前的服务器,并启动了与您显示的代码相对应的服务器.人们有时会被仍在运行的服务器的早期版本所迷惑,并且实际上并未包含他们认为正在运行的代码.
- 您是否100%确定要在所需的端口上运行服务器,该端口与您所使用的URL中的端口相匹配.
- 当其他所有方法都失败时,有时计算机会重新启动,以确保没有任何先前版本的内容正在运行.
- Are you getting any output in the console when you hit any of your expected routes?
- Can you prove that your server is running and your browser is hitting your route handlers?
- If you just do
console.log(req.query)
, what output do you get? - Are you absolutely sure that you've killed any prior servers and started the server that corresponds to the code you show. People sometimes get fooled by a prior version of the server that is still running and doesn't actually contain the code they think they are running.
- Are you 100% sure you are running your server on the desired port that matches the port in the URL you are using.
- When all else fails, sometimes a computer reboot will make sure no prior versions of anything are still running.
这篇关于Express req.query始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!