Explain to Mean.io beginner how Mean.io sample package's authentication works
REGARDING QUESTION A (on the use of exports
)
In Node.js, assigning values to the exports
object makes those values available to the code that require
s the source file.
For example, given file foo.js
:
exports.foo = "FOO";exports.bar = "BAR";
and file main.js
:
var foo = require('foo.js');console.log('foo=',foo.foo,'; bar=',foo.bar);
running node main.js
will output foo= FOO ; bar= BAR
.
See, for example, Node's module documentation or this write-up on require
and exports
.
REGARDING QUESTION B (on package "linking")
The answer to this question is the complement to the answer to Question A.
There is code to "link" the packages. It is the require
statement.
In your app.js
source code, the first line (reading var mean = require('meanio')
) will set the local variable mean
to whatever values are assigned to exports
object is when meanio.js
and/or the meanio
module is loaded.
Same with passport = require('passport')
. In that case, the local variable passport
will be equal to the value of exports
after index.js in the passport module is loaded.
REGARDING QUESTION C
I'm not entirely sure what you are asking here, but let me take a stab at it.
In this case:
1) var mean = require('meanio')
in line 1 "imports" the meanio module, such that the local variable mean
is more or less set equal to the value of exports
in the meanio module.
2) Module = mean.Module
in line 2 sets the local variable Module
equal to the value of mean.Module
, which must have been assigned in the meanio module.
3) var Access = new Module('access')
is instantiating an instance of the Module
class, assigning it to the local variable Access
.
4) Access.passport = passport
assigns the instance variable named passport
within the instance of meanio.Module
named Access
(to the value of the passport
module require
d on line #3)
5) Access.middleware = auth
assigns the instance variable named middleward
within the instance of meanio.Module
named Access
(to the value returned by require('./server/config/authorization')
in line 11).
I'm not familiar with the "meanio" module, but based on this code it looks like you are configuring the meanio.Module("access")
instance (named Access
) by assigning specific "magic" variable names.
In other words, rather than Access.passport = passport; Access.middleware = auth
you might have Access.setPassport(passport); Access.setMiddleware(auth)
or (rather than line 5) var Access = new Module('access',passport,auth)
.
That is, the author of the "meanio" module seems to have decided to use special variable names to configure the class rather than "setter" methods or parameters passed to the constructor. I assume that somewhere in the meanio code you'll find a reference to something like this.middleware
and this.passport
, where the code is assuming you have "filled in" those instance variables as happens in the last few lines in your code sample.
If you were to add Access.auth = auth
then all that would happen is that the Access
object would have a new attributed named auth
whose value is equal to the that of the local variable auth
.
If you used Access.auth
instead of Access.middleware
, I assume whatever code in the Access
class that is using this.middleware
will fail, since no value was ever assigned to Access.middleware
and Access.auth
is not one of the "magic" variable names that meanio is looking for.