Skip to main content

How to Build a Simple Blog Using Node.js

Getting Started

Make sure that you have Node.js and NPM installed on your machine, if not, visit the Node.js website to install the latest version.
Make sure you have Yarn installed globally:
npm install yarn -g
Let's start by creating a folder for our app.  In your favorite terminal run the following commands:
mkdir simple-blog
cd simple-blog
Now let's add a package.json file to import all of our dependencies for our app:
vim package.json
Add the following to our package.json file:
  "dependencies": {
    "cosmicjs": "^2.39.0",
    "express": "^4.15.2",
    "hogan-express": "^0.5.2",
    "nodemon": "^1.11.0"
  "scripts": {
    "start": "node app.js",
    "development": "nodemon app.js"

 It's a pretty light dependency list for a pretty light app.  So what we will install is:
1. The Cosmic JS Node Module to get our content from our Cosmic JS Bucket.
2. Express for our web app framework
3. Hogan for our template views
4. Nodemon for development
Our scripts are necessary for starting our app in production and development.
Run the following command to install our dependencies:

Building Your Blog

Next, let's begin building our blog pages.  Create a file titled app.js:
vim app.js
and add the following to app.js:
const express = require('express')
const app = express()
const hogan = require('hogan-express')
const http_module = require('http')
const http = http_module.Server(app)
app.engine('html', hogan)
app.set('port', (process.env.PORT || 3000))
app.use('/', express.static(__dirname + '/public/'))
const Cosmic = require('cosmicjs')
const helpers = require('./helpers')
const bucket_slug = process.env.COSMIC_BUCKET || 'simple-blog-website'
const read_key = process.env.COSMIC_READ_KEY
const partials = {
  header: 'partials/header',
  footer: 'partials/footer'
app.use('/', (req, res, next) => {
  res.locals.year = new Date().getFullYear()
// Home
app.get('/', (req, res) => {
  Cosmic.getObjects({ bucket: { slug: bucket_slug, read_key: read_key } }, (err, response) => {
    const cosmic = response
    if (cosmic.objects.type.posts) {
      cosmic.objects.type.posts.forEach(post => {
        const friendly_date = helpers.friendlyDate(new Date(post.created_at))
        post.friendly_date = friendly_date.month + ' ' +
    } else {
      cosmic.no_posts = true
    res.locals.cosmic = cosmic
    res.render('index.html', { partials })
http.listen(app.get('port'), () => {'==> 🌎  Go to http://localhost:%s', app.get('port'));
There are a few things happening here:
1. We are importing our essential modules: Express, Cosmic JS, setting our PORT dynamically, etc.
2. We are pointing to some partials: header and footer, you can reference these partials from the codebase on GitHub.
3. We view our app home page ('/') and query our Cosmic JS Bucket for Post Objects, set the friendly date, then return the index.html template.
4. We are also adding our data to this page in the form of a global data store: cosmic.  This data structure makes our template implementation super intuitive.

Adding Your Home Page Template Variables

This part of the process is the most fun because it shows you the power of Cosmic JS combined with a declarative, logic-less template system like Mustache.  Let's create a folder called views and add our index.html file:

mkdir views
cd views
vim index.html
Add the following to index.html:
{{> header }}
  <main class="container">
    {{# cosmic.objects.type.posts }}
      <div class="card" data-href="/{{ slug }}">
        {{# metadata.hero.imgix_url }}
          <div class="blog-post-hero blog-post-hero--short" style="background-image: url({{ metadata.hero.imgix_url }})"></div>
        {{/ metadata.hero.imgix_url }}
        <div class="card-padding">
          <h2 class="blog__title blog__title--small">
            <a href="/{{ slug }}">{{ title }}</a>
          <div class="blog__author">
            <div class="blog__author-image" style="background-image: url({{ }}?w=100)"></div>
            <div class="blog__author-title">by <a href="/author/{{ }}">{{ }}</a> on {{ friendly_date }}</div>
            <div class="clearfix"></div>
          <div class="blog__teaser droid">{{{ metadata.teaser }}}</div>
          <div class="blog__read-more">
            <a href="/{{ slug }}">Read more...</a>
    {{/ cosmic.objects.type.posts }}
{{> footer }}

What's happening here?
1. We pull in our header file with the Mustache template variable.
2. We pull in our Cosmic data using the Mustache variable {{ cosmic }} which has everything we need to layout our page's dynamic data.
3. We are looping through our {{ cosmic.objects.type.posts }} and rolling out our blog posts.
What I like about this approach is that the template is completely logic-less aside from some boolean queries and array looping.  It really does a great job of keeping our logic separate from our presentation (thanks Chris Wanstrath!)
If you want to build out the rest of the application, clone the GitHub repo and follow the README instructions.


This is the abridged version of the Simple Blog app available for download in the Cosmic JS Apps page.  The full codebase includes a single post page view as well as a page dedicated to each author's posts.  View the full codebase on GitHub and deploy this app in a few clicks from your Cosmic JS dashboard by installing the app to your Cosmic JS Bucket.



Popular posts from this blog

Design Tools to Help You Create Your Next Project- Part 3

CoolorsCoolorsis a super fast color scheme generator. You can explore thousands of pre-existing color schemes (each one features five colors). Or, you can generate your own in a matter of minutes. Once you go to the “generate” page, hit the space bar to start with a different color scheme, and then you can adjust each color’s hue, saturation, and brightness accordingly.

Web GradientsWeb Gradientsis a collection of almost 200 background gradients, created by the itmeo team. You can use each of these content backdrops for any part of your website. You’ll find a .PNG version of each gradient, as well as easy-to-copy CSS3 crossbrowser code. Bonus: there are even curated packs for Sketch & Photoshop.

Color Hunt On Color Hunt

MySQL Connector/Net 8.0.8-dmr has been released on 11th July, 2017

MySQL Connector/Net 8.0.8 is the fifth development release that expands cross-platform
support to Linux and macOS when using Microsoft’s .NET Core framework. Now, .NET
developers can use the X DevAPI with .NET Core and Entity Framework Core (EF Core)
1.0 to create server applications that run on Windows, Linux and macOS. We are very
excited about this change and really look forward to your feedback on it! MySQL Connector/Net 8.0.8 is also the seventh development release of MySQL
Connector/Net to add support for the new X DevAPI. The X DevAPI enables application
developers to write code that combines the strengths of the relational and document
models using a modern, NoSQL-like syntax that does not assume previous experience
writing traditional SQL. To learn more about how to write applications using the X DevAPI, see
For more information about how the X DevAPI is implemented in Connector/Net, see…

Get start with Vue.js

Getting Started The official guide assumes intermediate level knowledge of HTML, CSS, and JavaScript. If you are totally new to frontend development, it might not be the best idea to jump right into a framework as your first step - grasp the basics then come back! Prior experience with other frameworks helps, but is not required. The easiest way to try out Vue.js is using theJSFiddle Hello World example. Feel free to open it in another tab and follow along as we go through some basic examples. Or, you can simplycreate anindex.htmlfileand include Vue with: <scriptsrc=""></script> TheInstallationpage provides more options of installing Vue. Note that wedo notrecommend beginners to start withvue-cli, especially if you are not yet familiar with Node.js-based build tools. Declarative Rendering At the core of Vue.js is a system that enables us to declaratively render data to the DOM using straightforward template syntax: <divid="app"> …