Yeoman, Mongoose, and MongoDB Scaffolding

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

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 taskrunner to perform run/build/test tasks. If you want to use Gulp for the same, you can checkout Slush. Slush is also a Scaffolding tool but uses Gulp as the taskrunner.

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, run

yo mongoose

to scaffold a new project. 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


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 – consist of the DB configs and some options you can mess around with

models/post.js – is an example schema of a blog post. All the other models, which we are going to scaffold with the sub generator will appear here.

public/ – consist of the Javascript and CSS needed for the UI

index.js – consist of the default route, that will dispatch the index.html
post.js – consist of 5 key endpoints you need to interact with the posts collection

test/ – consists of the test for Post route and its methods

views/ – consists of all the templates & views sent to the client.

I recommend taking a peek at the following in order


to get a feel of where things go in a modular Express app. Once you are done, we will 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 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

And create those 3 files. And the models/article.js will consist of all the fields listed after the pipe symbol in the command. Sweet right?

To see the result run


and you can see the updated list of routes. Dig into the newly generated files and with practically zero efforts, 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.

Arvind Ravulavaru is a passionate web developer, likes experimenting with new and upcoming technologies. He also blogs at

  • Pingback: Getting started with MongoDB & Mongoose()

  • acveer

    after ‘yo mongoose’ scaffold, I am getting an exception for the default server url ‘/’, when we run ‘grunt’ to start the server. The app does get connected successfully to mongodb, but getting error with routes. From what I know, I could not figure out why the is coming as undefined. Created an issue here

    TypeError: Cannot read property ‘get’ of undefined
    at route.index (C:PAwsmongodbyoMongooseApp1routesindex.js:11:33)
    at Layer.handle [as handle_request] (C:PAwsmongodbyoMongooseApp1node_modulesexpresslibrouterlayer.js:76:5)
    at next (C:PAwsmongodbyoMongooseApp1node_modulesexpresslibrouterroute.js:100:13)
    at Route.dispatch (C:PAwsmongodbyoMongooseApp1node_modulesexpresslibrouterroute.js:81:3)
    at Layer.handle [as handle_request] (C:PAwsmongodbyoMongooseApp1node_modulesexpresslibrouterlayer.js:76:5)

  • acveer

    your package slush-mango did it (gulp). Thank you. I was able to get all the default routes along with article also. Tests also pass. Its pretty cool, that all the CRUD operations & tests are generated with no effort. I sincerely appreciate your tutorials & effort. You are an awesome contributor.

  • Britton Winterrose

    Super helpful, thank you. So many tutorials skip over this part and the way you explain was very clear.