我正在使用这样的快速路由,我希望我的 url 最初包含查询字符串。
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', {});
});
Run Code Online (Sandbox Code Playgroud)
但是,无论我在 /us01 之后附加什么查询字符串,我都不会打印我的查询字符串。例如,"localhost:9200/us01?a=1" req.query 应该得到我 {a:1},对吗?这是普遍现象吗?我在这里缺少什么?
我的 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'));
});
Run Code Online (Sandbox Code Playgroud)
我的 indexController.js 有:
$stateProvider
.state('us01', {
url: '/us01',
templateUrl: '/us01'
}).state('benchmark', {
url: '/benchmark',
templateUrl: '/benchmark'
})....
Run Code Online (Sandbox Code Playgroud)
这个简单的代码:
const express = require('express');
const app = express();
app.get('/us01', function(req, res) {
console.log(req.query);
res.send("ok");
});
app.listen(80);
Run Code Online (Sandbox Code Playgroud)
然后,通过访问http://localhost/us01?a=1在控制台中生成以下输出:
{ a: '1' }
Run Code Online (Sandbox Code Playgroud)
或者,如果我使用:
console.log('query: ' + JSON.stringify(req.query));
Run Code Online (Sandbox Code Playgroud)
然后,我在控制台中看到:
query: {"a":"1"}
Run Code Online (Sandbox Code Playgroud)
所以,显然您的代码中还有其他问题。
"localhost:9200/us01?a=1" req.query 应该得到我 {a:1},对吗?
query: {"a":"1"}如果您显示的代码正在本地主机的端口 9200 上运行,它应该会帮助您。
这是常见的事情吗?
不。除了您显示的代码之外,还有其他东西被破坏了,因为那段代码没有任何问题。
我在这里缺少什么?
要检查的事项:
console.log(req.query),你会得到什么输出?| 归档时间: |
|
| 查看次数: |
7748 次 |
| 最近记录: |