简体   繁体   中英

What is the reason of adding all modules to express index file app.js?

I often see that all modules used by an application (Express.js) are added in the very beginning of the index file "app.js". Like this:

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

And that's it, nothing deals with them in "app.js". They are used somewhere else, for example this modules may be required route file "/routes/login.js", where their addition is duplicating.

What's the matter of adding all modules in the "app.js" insted of adding them only where it really needed? Is it a part of convention or there are some real needs?

Most likely people begin writing their project in a single file. They include everything there, then, when they split code into multiple files, they forget to remove the require s.

The only semi-real benefit I can think of is preloading the modules at the beginning, because technically when you require a module twice it should resolve to the same object and all of its' initialization should execute only once. But that's stretching it, really. I think people just forget.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM