Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Build a REST API With Express Building API Routes in Express Using Middleware in Express

Why does req.query return undefined?

// Postman----------------------------- // [GET] localhost:3000/color?=green [SEND] //------------------------------------------ var express = require('express');

var app = express(); app.use(function (req, res, next) { console.log("The leaves on the ground are", req.query.color); next(); });

var port = process.env.PORT || 3000;

app.listen(port, function () { console.log(App is running on port ${port}!); });

I believe it should be

req.params.color

3 Answers

Benjamin Barslev Nielsen
Benjamin Barslev Nielsen
18,958 Points

The request seems strange:

localhost:3000/color?=green

It should work with this request instead:

localhost:3000?color=green

This should solve the request problem, but you also need to fix the console.log in the last line, since you forget to use "" around the string, so a possible fix could be:

app.listen(port, function () { console.log("App is running on port " + port + "!"); });
J. MORENO
J. MORENO
12,550 Points

The request should have been - http://localhost:3000/?color=green

The ? was misplaced.

J. MORENO
J. MORENO
12,550 Points

after cleaning up the code and using the proper request:

var express = require('express');

var app = express(); var port = process.env.PORT || 3000;

app.use(function (req, res, next) { console.log("The leaves on the ground are", req.query.color); next(); });

app.listen(port, function () { console.log(App is running on port ${port}!); });