What we’ll be building
View the demoView the full codebase on GitHubInstall and deploy the website in a few clicks
When you’re laser focused on writing songs, getting the most out of your studio time, and finding gigs, you can’t be bothered with setting up a static site for your band or dealing with abstruse CMS systems. When you want easy access to your data, want to know where everything is, and want to be able to write a simple, predictable app for yourself, Cosmic JS should be the most obvious choice.
Cosmic JS is an API-first CMS, meaning it is language independent, database independent, and practically everything-else independent. This is great for a small project like this one because we can extend it quickly with any language or framework in the future and we can define data structures that are only as complex as we need them to be.
Our band site needs to have the following features:
Our site will be built as an Express app, integrate with Mailchimp to handle email signups, and will have all of its data served entirely from Cosmic JS. It will be deployed through Cosmic as well.
We’ll use three Object types in Cosmic to structure our data.
We’ll set up each Object type with predefined metafields like so:
**Tour DatesMetafields**Location (text)Venue (text)Date (date)Ticket Link (text)
**GalleriesMetafields**Photos (repeater) - Photo (file) - Date (date)
**VideosMetafields**Embed Code (text)
(The Settings type won’t have predefiend metafields — we’ll set those as we need them)
To save time on boilerplate, we’ll use Yeoman and the Express Generator (which builds on Express’ official generator) to get started. If you don’t have Yeoman installed, run npm i -g yo
. Then install the generator with npm i -g generator-express
and run it with yo express
. Follow the instructions to set up your project under a new directory (say CosmicUserBlog
), install the Basic version, and use Handlebars for your view engine.
Your directory structure is now this:
Part 2. Installations
We’ll be using the following packages:
You could install these with npm, but I advocate for Yarn. It’s significantly faster and we have no time to waste. So install Yarn (on macOS we’ll do a brew install yarn
) then run yarn add async axios cors cosmicjs moment truncate-html lodash
.
Within app.js
, import the packages we just installed and declare a config
object to be used with the Cosmic JS client:
We assign it to app.locals
to use it in our routes.
We want to be able to display certain data globally throughout the site and we’ll store it in a Cosmic JS object to make it easy to edit, rather than having to redeploy our site everytime something needs changed. In your Cosmic dashboard, make a Settings
object called `Site Settings`
.
The Main Layout
The last step before we get building is to make a few tweaks to the main Handlebars layout, in which we need to link to Font Awesome, Normalize.css, and change the title. Post-changes, main.handlebars
will look like this:
Every page on the site will have a header, so we’ll build that first. For the same reason, we’ll make it a partial. It will feature links to our social media, a signup form for our mailing list, our logo, and the main navigation.
Add this to partials/header.handlebars
:
This gives us a responsive header with a mobile dropdown menu where we need to build out nav-links
, signup-form
, and social-links
as their own partials. Again, settings.logo
(and other data) will be passed from the route through req.locals
.
For the sake of not repeating ourselves, we create a partial with all of our navigation links in partials/nav-links.handlebars
.
The same goes for our social media links, except the way we pass the data will make the construction slighly more interesting. Because the link element is the same for each account, save for the title
, text, and href
, we'll use the Handlebars each
helper to iterate over all of our accounts.
Finally, we need a form to handle adding new subscribers to our Mailchimp list. We’ll simply gather their email address and process it later in a POST request to /signup
.
With all of the Header partials complete, the header itself is complete, and we essentially get the Footer for free:
Notice the #getYear
helper. We'll define this later in app.js
and us it to return the current year as a string.
The home page will be a glimpse into what’s on the rest of the site. In the top half we’ll show our header and a big CTA section with buttons linking to our Tour Dates and photo gallery. In the bottom half we’ll show three upcoming shows, a blurb of our bio, and a footer.
This article is an abbreviated version of the original article posted on Cosmic JS by Matt Cain
Before deploying — which is just pushing to a Github repo and following the instructions on the Cosmic dashboard — make sure you compile the Sass styles with gulp sass
Using Cosmic JS, Express, and Mailchimp, we’ve built an accessible, functional website for our band that lets us update information, tour dates, photo galleries, and videos on the fly, all without having to redploy the site with every change. Cosmic’s API first approch let us do this simply, without a database, and with a great dashboard to manage our data whenever, wherever.
With how quickly we’ve been able to build our app and with the simplicity of deploying and maintaining it, it’s clear that Cosmic JS is one of a kind in its API first approach to content management. Clearly, CosmisJS is a money maker.
Matt Cain builds smart web applications and writes about the tech used to build them. You can learn more about him on his portfolio.