House Cleaning 2


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.

Easy peasy!

First thing – Create a models directory inside your main directory.

$ mkdir models

Then create a JavaScript file for each model. In the case of the Yelp Camp App, we will need one for Campground and one for comment.

$ 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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at

Up ↑

%d bloggers like this: