We are going to build a simple Node.js application that allows us to view a list of articles and create articles that will be saved to a MongoDB. Then when we’re finished building our app we will deploy it to Heroku. I tried to use the most popular tools surrounding Node.js so that when you have questions in the future, there will be plenty of resources online to help you.
-
Yeoman - Scaffolding tool that we will use to create the MVC base for our app
-
Express.js - Node.js framework that we will use for basic routing and html templating
-
Handlebars - Template engine we will be using with Express. It does not allow for complex logic in views which helps to keep our concerns separate.
-
MongoDB - Document-oriented NoSQL database. Easy to setup and tons of articles, blog posts, and StackOverflow questions.
-
Mongoose- Object Document Mapper used to interact with MongoDB. We will use Mongoose to create Schemas and Models for our data.
-
Bootstrap- CSS framework that will help us avoid writing any CSS.
-
Gulp- Build tool that allows you to automate just about any task.
-
Bower- Front end package manager
-
npm install yo -g
-
npm install generator-express -g
-
yo express
-
Would you like to create a new directory for your project? Yes
-
Enter directory name: nodegreenville-news
-
Select a version to install: MVC
-
Select a view engine to use: Handlebars
-
Select a css preprocessor to use: None
-
Select a database to use: MongoDB
-
Select a build tool to use: Gulp
-
-
cd nodegreenville-news
-
atom . (or open the project in your favorite text edit or IDE)
-
Under the Resources tab, scroll down to Add-ons and search for mLab
-
Provision a Sandbox - free account
-
Under Settings section click Reveal Config Vars and copy MONGODB_URI
-
npm install dotenv --save
-
Create file ".env" in the root of project
-
Add the line ".env" to .gitignore file
-
Open config/config.js and add:
require("dotenv").config({silent:true});
-
Still in config.js replace the development db url with:
process.env.MONGODB_URI
-
Test out everything by running gulp command and visiting http://localhost:3000
-
Open controllers/home.js and change the rendered title from ‘Generator-Express MVC’ to ‘NodeGreenville News’.
-
bower install bootstrap --save
-
Open views/layouts/main.handlebars and add:
<link rel="stylesheet" href="/components/bootstrap/dist/css/bootstrap.min.css">
-
Now we need to add the Bootstrap JavaScript file and jQuery. Just before the close body tag add:
<script src="/components/jquery/dist/jquery.min.js"></script> <script src="/components/bootstrap/dist/js/bootstrap.min.js"></script>
-
Add div with the class "container" around {{{ body }}}
<div class="container"> {{{ body }}} </div>
-
Above the div containing {{{ body }}} we need to add a Bootstrap navbar:
<nav class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <a class="navbar-brand" href="/">News</a> <ul class="nav navbar-nav"> <li class="active"><a href="/new">New Article</a></li> </ul> </div> </nav>
-
Inside views folder create new.handlebars and add:
<h1>New Article</h1>
-
Inside controllers folder create new.js and add:
var express = require('express'), router = express.Router(), mongoose = require('mongoose'), Article = mongoose.model('Article'); module.exports = function (app) { app.use('/new', router); }; router.get('/', function (req, res, next) { res.render('new'); });
-
Back on new.handlebars add the form:
<form action="/new" method="POST"> <div class="form-group"> <label for="title">Title</label> <input type="text" class="form-control" name="title" placeholder="Title"> </div> <div class="form-group"> <label for="content">Text</label> <textarea class="form-control" name="text" placeholder="Text"></textarea> </div> <button type="submit" class="btn btn-default">Submit</button> </form>
-
Inside controllers/new.js add the POST endpoint:
router.post('/', function (req, res, next) { var article = new Article({ title: req.body.title, text: req.body.text }); article.save(function(err) { if (err) { return next(err); } res.redirect('/'); }); });
-
Open views/index.handlebars and add the feed:
{{#each articles}} <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">{{this.title}}</h3> </div> <div class="panel-body"> {{this.text}} </div> </div> {{/each}}
-
npm install bower --save
-
Open package.json to add bower script above the start script:
"postinstall": "./node_modules/bower/bin/bower install", -
git init
-
git add -A && git commit -m "First Deploy"
-
Configure git remote for Heroku:
heroku git:remote -a nodegreenville-news-demo -
git push heroku master
-
heroku logs
-
Update config/config.js and add the production db URL
process.env.MONGODB_URI
-
git add -A && git commit -m "Updated production DB URL"
-
git push heroku master