开发者

Node.js and Mongoose, too slow to put Schema definitions in separate files?

开发者 https://www.devze.com 2023-03-20 08:24 出处:网络
Playing around with Node.js and I really want to keep开发者_如何学Go my file structure organized.Normally in a project in another language I would have a structure like this:

Playing around with Node.js and I really want to keep开发者_如何学Go my file structure organized. Normally in a project in another language I would have a structure like this:

  • Node App Directory
    • App.js
    • Controllers
      • UserController.js
      • OtherController.js
    • Models
      • UserModel.js
      • OtherModel.js

The (potential) problem I see is this: I use Mongoose and MongoDB for my database, in order to define a "Model" I need a Mongoose Schema, in order to do that I have to connect to the database with Mongoose in each of the Controllers and Model files.

I know so little about these technologies, this could be lightning quick and never be an issue and I wouldn't have a clue. Or is this something I should avoid and just shove everything in one (very large) file?


You don't need to connect to mongo in every controller/model file. Just do it once in App.js and you're done.

0

精彩评论

暂无评论...
验证码 换一张
取 消