Express req.query always empty
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:
- 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.