Defining Mongoose Models in Separate Module

后端 未结 2 418
情歌与酒
情歌与酒 2020-12-12 12:35

I would like to separate my Mongoose models in a separate file. I have attempted to do so like this:

var mongoose = require(\"mongoose\");
var Schema = mongo         


        
2条回答
  •  轮回少年
    2020-12-12 13:08

    The basic approach looks reasonable.

    As an option you could consider a 'provider' module with model and controller functionality integrated. That way you could have the app.js instantiate the provider and then all controller functions can be executed by it. The app.js has to only specify the routes with the corresponding controller functionality to be implemented.

    To tidy up a bit further you could also consider branching out the routes into a separate module with app.js as a glue between these modules.

提交回复
热议问题