Yeoman, Mongoose, and MongoDB Scaffolding

In our previous post, we talked about getting started with Mongoose and MongoDB. In this post, we’ll show you how to use Yeoman and scaffold a new Mongoose/Express project for MongoDB.

Yeoman is a scaffolding tool, that scaffolds out projects using Grunt, Bower, and Node. There are times when you end up cut and pasting boilerplate code around to create a new project. This is precisely what Yeoman does, but with a single command and a few awesome generators.

Yeoman uses Grunt as the task runner to perform run/build/test tasks. If you want to use Gulp for the same, you can check out Slush. Slush is also a Scaffolding tool but uses Gulp as the task runner.

Getting Started with Yeoman

To make our lives easy, we will be using a Super Awesome Yeoman Generator named generator-mongoose, which will help us in setting up a new project as well as help us in scaffolding schemas.

This generator uses Express.js as the server, HTML for templating and a tinge of Bootstrap CSS to make things look good.

Let’s create a new folder and name it yoMongoose. CD into the folder and run the following:

To install Yeoman:

[sudo] npm install -g yo

To install generator-mongoose:

[sudo] npm install -g generator-mongoose

and finally, to scaffold a new project, run:

yo mongoose

Fill in the question like:

[?] Database Name: (myDb) myTestDB
[?] Database Host: (localhost) localhost
[?] Database User: {hit return}
[?] Database Password: {hit return}
[?] Database Port: (27017) 27017
[?] Will you be using heroku? (Y/n)  n

And Yeoman will go off and scaffold a new project for you. Your folder structure should consist of a /node_modules folder and a public/bower_components. If you do not see either of them, please run npm install and bower install.

To run the app, execute:

grunt

This will start off the express server and launch the home page in your default browser. The default page you see is a list of routes configured in the application.

Back to the folder and let’s have a quick walkthrough of the app.

  • config/db.js

    Consists of the DB configs and some options you can mess around with.

  • models/post.js

    An example schema of a blog post. All the other models we’re going to scaffold with the sub-generator will appear here.

  • public/

    Consist of the JavaScript and CSS needed for the UI.

  • routes/
index.js

    Consist of the default route that will dispatch the index.html
post.js, and consists of 5 key endpoints you’ll need to interact with the posts collection.

  • test/

    Consists of the test for Post route and its methods.

  • views/

    Consists of all the templates and views sent to the client.

To get a feel for where things go in a modular Express app, I recommend taking a peek at the following in order:

config/db.js
models/post.js
routes/post.js
app.js

Once you’re done, we’ll scaffold another model named articles using the sub-generator.

Back to terminal/prompt and run:

yo mongoose:schema "article|title:String,excerpt:String,content:String,published:Boolean,created:Date"

The above command will create those 3 files and result in:

Your creating a schema for article
With the fields: title,excerpt,content,published,created
starting request to schematic for test mock data...
create routes/article.js
create models/article.js
create test/test-article.js

The models/article.js will consist of all the fields listed after the pipe symbol in the command. Sweet right?

To see the result and an updated list of routes, run:

grunt

Dig into the newly generated files, and with practically zero effort, we were able to generate an Express/Mongoose app.

Hope you got a basic understanding of Scaffolding Express/Mongoose apps.

Thanks for reading. Do comment.
@arvindr21