Skip to content
🐨 A starter kit for a slightly opinionated koa project.
HTML JavaScript CSS
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
assets
controllers
helpers
models
views
.eslintignore
.eslintrc
.gitignore
README.md
config.example.json
index.js
package-lock.json
package.json
routes.js

README.md

🐨 koa-starter

A starter kit for a slightly opinionated Koa project.

Opinions

I built this Koa starter kit because there's a number of packages I like to use when starting a new project. The major ones are:

I'm also including goodies from:

Prerequisites

Installation

  • Clone down the repository.
git clone https://github.com/snollygolly/koa-starter.git
  • Install packages (from inside the koa-starter folder).
npm install
  • Create your config. There's a config.example.json file in the root. Edit it to include all your values for the site and your OAuth information. Save it as config.json and leave it in the root.

  • If you want to use Google Analytics, set config.site.analytics to your Tracking ID and make sure the analytics partial (analytics.hbs) contains the correct Universal Analytics tracking code. If you don't want to use Google Analytics, remove that property or set it to false.

  • Start it up.

npm start
  • Enjoy!

Extras

While koa-starter isn't a framework, I've added a few small extras to make getting your project up and started as easy as possible.

Including assets from view rendering

When you render you view, you can use the following properties to link to assets.

  • Scripts: This array contains scripts living in /assets/js that you'd like to link to. The .js will be added for you.
await ctx.render("index", {
  title: config.site.name,
  user: user,
  scripts: ["index", "extra"]
});
  • Vendor JavaScript: This array contains links to external files you'd like to link to. The .js will not be added for you.
await ctx.render("index", {
  title: config.site.name,
  user: user,
  vendor_js: ["http://vendor.com/cdn/lib.min.js"]
});
  • Vendor CSS: This array contains links to external files you'd like to link to. The .css will not be added for you.
await ctx.render("index", {
  title: config.site.name,
  user: user,
  vendor_css: ["http://vendor.com/cdn/lib.min.css"]
});

Error Handling

Thrown errors are caught in the error handling middleware in index.js. By default, all errors will render the error.hbs view with information about the error. It's possible to set ctx.state.api equal to true in the controller to tell the error handling middleware that this route is an API endpoint, and the error should be sent in JSON instead of rendered as a view.

You can’t perform that action at this time.