A starter kit for a slightly opinionated Koa project.
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:
git clone https://github.com/snollygolly/koa-starter.git
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
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.
When you render you view, you can use the following properties to link to assets.
/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"]
});
.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"]
});
.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"]
});
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.