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 Express Basics Middleware Middleware Sequence and Routing

Nicholas Gaerlan
Nicholas Gaerlan
9,501 Points

Is req an object with more global scope?

In the video we set req.message to "This message made it!" in one block of code and then use it an entirely different block of code. In general, I would think it's bad practice to set a variable in one method call and then have that same variable available in an entirely separate block. I guess this is like the Dice project where the value of the dice was lifted out of the block scope and multiple instances of the dice object all got there value from one global object storing it. Still... goes against my instincts having a something look like a block scoped variable but behave like a global variable.

4 Answers

Steven Parker
Steven Parker
231,275 Points

You'll notice that "req" is passed as an argument in the standard calls and is not a global object. This is a good practice technique known as "dependency injection" and avoids the use of a global object which (as you point out) would not be a good practice.

Nicholas Gaerlan
Nicholas Gaerlan
9,501 Points

Thanks! You'd make a great mentor!

But how is the req.message in the first function passed as the "req" that gets passed into the 2nd function?

Steven Parker
Steven Parker
231,275 Points

I'm not sure which one you are calling "2nd function" but I believe all the functions that use "req" and/or "res" get them passed in as arguments.

The request object is not a global.

It does however pass on to the next - pun noted, middleware function used by the application in sequential order, as long as they are on the same route and method.

Nicholas Gaerlan
Nicholas Gaerlan
9,501 Points

@Steven Parker @Peter Lu the video at 3m:54s shows him defining the req.message in one middleware block and then in an entire different middleware block, he prints the message. the first argument of the middleware callback has got to be a global object within its module (we import it, but the lessons never cover the structure of that module), that's the only explanation I can think of. So instead of an instance, we're dealing with a pointer/global object. It's not too bad because it's enclosed within its own module that we have to import. However, shared mutable state will probably always make me cringe.

Steven Parker
Steven Parker
231,275 Points

The "req" in this case is passed from the middleware (which received it as an argument) to the callback (also as an argument). It's never a global.

nico dev
nico dev
20,364 Points

Sorry to step in, and hope not to have misunderstood anyone or anything, but just to prove Steven's point, try adding another block of code like the one mentioned, this one:

app.use((req, res, next) => {
    console.log(req.message)
    next()
})

Now, first add it before the '/hello' route. Check the console. Message will be printed twice, meaning the value of req.message was somehow passed from one to the other, right?

Now, cut it and paste it at the end of the app. It's printed only once. Meaning it was not passed, and especially that the req object is not any kind of global here.

I think the reason of this is that the next arguments are providing a middleware function with access to the next one (and only to the next one, to be more specific). As explained here:

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.

Rob Inman
Rob Inman
4,516 Points

So is the request from the second middleware function the same request from the first middleware function just with a new property?

A request is made -> that request is handled in the first middleware function -> that same request is passed off to the second middleware function -> a response is issued