express - Node.js best practice for require -


i creating express application , using mongoose save data.

i created user model (username&password). needs able saved. use require('mongoose') in models/user-model.js. in route user want able users or find some. need require('mongoose') there well. in main js file (app.js) create connection database there require('mongoose') there well.

it works well, best way require mongoose in of these files? or there better way this?

well, "best" hard certain. but, you're doing common-enough practice , should fine in cases.

the 1st time mongoose required, it'll cached subsequent requires:

modules cached after first time loaded. means (among other things) every call require('foo') same object returned, if resolve same file.

multiple calls require('foo') may not cause module code executed multiple times. [...]


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -