In our previous post we talked about getting started with Mongoose and MongoDB. In this post, we 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 ‘n 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
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 – 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
routes/ 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
config/db.js models/post.js routes/post.js app.js
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