0

What does ".Strategy" do here? Is it Node? Is it Passport?

var LocalStrategy = require('passport-local').Strategy;

Everything up to '.Strategy' part I understand. I just want to know what '.Strategy' does. I have checked the documentation on passport-local module on npm. I have also checked Passport's documentation, and it is just used in code snippets. No explanation is provided.

I am working with the MEAN stack and we are using Passport to authenticate users.

UhHuhOkSure
  • 147
  • 7
  • Did you look at https://www.npmjs.com/package/passport-local? Search for *"passport strategy"*? I seem to be finding a fair bit of documentation. – jonrsharpe Nov 10 '18 at 18:08
  • Yeah, that's the first place I looked. It doesn't exactly tell me what ".Strategy" does though. Everything leading up to the ".Strategy" part I understand. – UhHuhOkSure Nov 10 '18 at 18:13
  • What do you mean what it does? You can see the implementation, it's open source. Or look at the abstract version: https://github.com/jaredhanson/passport-strategy – jonrsharpe Nov 10 '18 at 18:16
  • I didn't know to look in the local strategy source code because there wasn't anything in the documentation about it. As far as I could tell, it could have been a Node thing or a Passport thing. – UhHuhOkSure Nov 10 '18 at 18:31
  • But you can see where it's coming from when you require it, what other information would you need to find it? You can see in the index file it comes from strategy.js, which seems logical. – jonrsharpe Nov 10 '18 at 18:32
  • Require returns/runs something, right? What's returned is everything between the assignment and dot operator. If I had questions about what was returned, I would know to look in the passport-local source code. But why would that mean Strategy is necessarily part of passport-local? It's doing something to the returned object. I've seen Node code come after the dot operator on one of these require expressions – UhHuhOkSure Nov 13 '18 at 02:19
  • You *do* have a question about what's returned, you want to know what its Strategy property does. It's not clear what you mean by *"Node code"*, all of this is JavaScript code running on Node. – jonrsharpe Nov 13 '18 at 07:36

1 Answers1

0

If you look at the sources of passport-local index.js you'll see it exports the same thing directly and in exports.Strategy.

When you do require('passport-local).Strategy you import the export defined in exports.Strategy, but it's really the same to do just require('passport-local') in this case because the same constructor is exported directly from the module.

If you define a module like this:

var Thing = { foo: () => 'bar' };

exports = module.exports = Thing;

exports.Thing = Thing;

you can use it in many ways:

const Thing = require('./module');
console.log(Thing.foo());

works, as does

const Thing = require('./module').Thing;
console.log(Thing.foo());

and with both imports you can actually call also

console.log(Thing.Thing.foo());

If you remove the exports.Thing = Thing; part of the module, then

const Thing = require('./module').Thing;

does not work anymore.

The exports cause confusion often. You could take a look of Node docs or eg. this answer.

vesse
  • 4,871
  • 26
  • 35