Express req.query always empty

六月ゝ 毕业季﹏ 提交于 2020-08-27 21:32:11

问题


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', {});
});

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?

My 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'));
});

My indexController.js has :

$stateProvider
        .state('us01', {
            url: '/us01',
            templateUrl: '/us01'
        }).state('benchmark', {
            url: '/benchmark',
            templateUrl: '/benchmark'
        })....

回答1:


This simple code:

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

app.get('/us01', function(req, res) {
    console.log(req.query);
    res.send("ok");
});

app.listen(80);

Then, accessed by http://localhost/us01?a=1 produces this output in the console:

{ a: '1' }

Or, if I use:

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 should get me {a:1}, correct?

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?

Things to examine:

  1. Are you getting any output in the console when you hit any of your expected routes?
  2. Can you prove that your server is running and your browser is hitting your route handlers?
  3. If you just do console.log(req.query), what output do you get?
  4. 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.
  5. Are you 100% sure you are running your server on the desired port that matches the port in the URL you are using.
  6. When all else fails, sometimes a computer reboot will make sure no prior versions of anything are still running.


来源:https://stackoverflow.com/questions/47822064/express-req-query-always-empty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!