Reputation: 202
I've picked up a project from another developer, uses the typical MEAN stack with the entry point being server.js.
Now, in server.js, the module that does:
var express = require('express');
var app = express();
var passport = require('passport');
There are another 2 lines of code that look like they are doing some sort of routing but I can't figure out what it actually means:
require('./routes.js')(app, passport);
require('./apiRequest/authenticate')(app, passport);
I'm confused because it looks like require()
is called from the global scope, whereas all the other routing methods are called off app, i.e app.use()
. Can someone explain what the sets of parameters mean, and why are there two sets also where is require()
called from, is it provided by Express?
Upvotes: 1
Views: 862
Reputation: 5413
routes.js
and apiRequest/authenticate
are two local (project) modules / js files that are basically required here.
express
and passport
are node modules/libraries that are provided from npm_modules, via node module resolution.
app
is simply an express instance created by invoking the express module/default function.
The parameters passed to the required local modules (routes and authenticate) are just parameters passed to those modules (default exported function) that can be used further in those files (e.g. if you look in routes.js you will probably see that they use app.use(...
, where app is given as param as well as the passport module)
To explain the syntax require('./routes.js')(app, passport);
more clearly:
require
- node OOB function for importing modules into the current file/modulerequire('./routes.js')
resolves the default export from the routes.js file which in this case is a function...(app, passport)
this function (from above point) is then invoked with the provided params (which were previously defined here - i.e. imported with require)Upvotes: 1