5

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'
        })....
Chloe.C
  • 51
  • 1
  • 1
  • 5

1 Answers1

4

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.
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Hi. Thanks for your detailed instructions. I have edited to include my app.js and some other files that I think relate to the routing. I am sure I am running on the correct port. I have tried console.log(req.query) directly also, just output [object Object]. That's why I tried to add JSON.stringify. Do you think the files I have shown have any interference with the req.query? Maybe something deal with dependencies? – Chloe.C Dec 14 '17 at 23:50
  • @Chloe.C - Something is screwy if `console.log(req.query)` is producing `[object Object]`. That should never happen because that's not how `console.log()` outputs an object. What exactly are you running this code with? Is it node.js? What exact version? – jfriend00 Dec 14 '17 at 23:55
  • @Chloe.C - I'd suggest you make a single app file with the code in it in my question and run that (you can change the port number if you need to). Let's figure out if it's your node.js/express environment that has a problem or if it's your code. – jfriend00 Dec 14 '17 at 23:56
  • I think the way it work on my machine is console.log(JSON.stringify(req.query). I got it work for app.post. In that way, I was using $state.go('',{param1:a,param2:b}) and I get my params in app post's console log. – Chloe.C Dec 15 '17 at 00:10
  • I find out the problem. There is a weird '/#/' append to my path every time. If I manually delete it, my query shows up. – Chloe.C Dec 15 '17 at 00:34
  • 2
    @Chloe.C - Well, you need to either post your own answer or delete your question because it shouldn't just be left hanging without an accepted answer. – jfriend00 Dec 15 '17 at 01:35