Clean Up Your Project with Modules (Adv Node & Express)

The question is not about how to complete, but why is it works! (link to the challenge)

As I remember, when a module required (or imported) it’s evaluated once and the result is usually assigned to a variable. In all three modules (server, routes, auth) these are different requires and different variables. So why settings made in different modules on different instances of e.g. passport does work well together? And consequent question would be what if user want to have different passports in modules for his own purposes. What should he do to achieve it? In the case of app object (and DB btw) it is passed as an argument when module is instantiated.

I’m not sure I understand.

As I remember, when a module require d (or import ed) it’s evaluated once and the result is usually assigned to a variable.

Sure.

So why settings made in different modules on different instances of e.g. passport does work well together?

If I understand, you are asking why, if you import passport into different files, why does one affect the other?

It would be because you are importing an object, which is a reference type. Primitives are passed/imported by value, but all else (objects, arrays, functions, etc.) are passed by reference. A reference is just an address in memory. There is only one instance of passport but different files have different copies of references.

If I buy a house and it’s a fixerupper, I’m going to ask different workers to work on it. I will write down the address on a piece of paper for each of them. There is still just one house, but they each have a different copy of the address. If one goes and makes a change to the house, it affects the house for all the others because there was always just one house.

Indeed, that’s the question.

Sure. Then why app is passed to the modules (routes, auth) on its require/evaluation instead of requiring it by reference as done with passport?

Thank you for clarifications, this thing really confuses my mind.

(And is it possible to instantiate two passports for example?)

It’s confusing. That’s why this job doesn’t pay minimum wage. That’s a good thing.

(And is it possible to instantiate two passport s for example?)

I’ve been speaking more generally and based on my understanding about how these modules usually work. We’d have to dig deeper into the module to see how it is setup - I haven’t used express or passport since I did FCC, I don’t do either at my current job. But I imagine it got set up the way it needs to be to work properly. I would recommend not getting too caught up in low level details until you get some more experience with modules, node, etc. Don’t get me wrong, you can learn a lot there, but it can distract you from your objective.

Probably the answers is in the methods that required module have. I remember that app indeed is an instantiation via call () of express module. And passport can have it’s own different lay-out for such things.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.