This is another cleaning up the Yelp Camp App as I go post! My app.js is looking a mess, there is way too much going on and we need to separate some bits out.
This time I’m going to take the mongoose models/schema out of my app.js and put them in separate files, in a models directory.
First thing – Create a models directory inside your main directory.
$ mkdir models
$ touch campground.js $ touch comment.js
In each file we will have to require mongoose, cut and paste the schema from app.js and then export the schema with a name that can be used to require it in app.js.
Here is my file for campground:
Lastly, we have to go into app.js and require each of the models.
var express = require("express"), app = express(), bodyParser = require("body-parser"), mongoose = require("mongoose"), passport = require("passport"), LocalStrategy = require("passport-local"), Campground = require("./models/campground"), Comment = require("./models/comment"), User = require("./models/user"), seedDB = require("./seeds");
Now our app.js file is a little bit less crowded and it’s easier to interact with individual models.