node.js - What is the purpose of the `(app)` in `require(controller)(app)`? -
i'm new node, blah blah
i'm looking through code found, , encountered lines
var app = express(); var glob = require('glob'); var controllers = glob.sync(config.root + '/app/controllers/*.js'); controllers.foreach(function (controller) { require(controller)(app); });
i understand goes , gets filenames of every file in /app/controllers/
ends .js
, , sticks them in array, iterates on each 1 , calls require on it. makes sense, , looks nice way of not requiring each 1 individually. question what's last (app)
for? tried looking @ node documentation, there no require()()
function. editing out (app)
leaves code working fine no errors, doesn't load controllers. if had take guess, 'multiplying' app found controller? why app.require(controller)
not suitable option?
thanks in advance
require
part of node , how modules loaded. when edit out (app)
, still loading controllers because haven't passed app
object on each controller, fail silently or return controller object doesn't work.
so in controller (probably route being created or other similar stuff) needs know app
object , has passed controller because app
isn't in global scope. may want review node docs module , globals clear way more 1 question.
Comments
Post a Comment