简体   繁体   中英

NodeJs-Express-server: is there a difference between using require in the beginning and later?

I'm running a NodeJs-Express-server. Is there a difference between using require in the beginning or later?

Is there a performance difference between:

var routes = require('./routes/index');
var admin = require("./routes/admin");
// a lot of code
app.use('/', routes);
app.use('/admin', admin);

... and ...

// a lot of code
app.use('/', require('./routes/index'));
app.use('/admin', require("./routes/admin"));

I do always see people writing 'require' in the beginning. But why?

They are the same, for your purposes - however, people prefer a few things;

They prefer that errors associated with files not existing happen early on instead of at an unexpected time, They prefer that they can see require calls at the top of the file.

require is analogous (although not the same) as similar features in other languages such as include or using.

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