Skip to content

Commit

Permalink
updating middleware and asset linking
Browse files Browse the repository at this point in the history
  • Loading branch information
snollygolly committed Jan 14, 2019
1 parent 1db00b1 commit 6cb41b5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
46 changes: 44 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ I built this Koa starter kit because there's a number of packages I like to use
* [Koa v2](http://koajs.com/)
* [Passport](http://passportjs.org/)
* [Handlebars](http://handlebarsjs.com/)
* [Bootstrap v4 Beta](http://getbootstrap.com/)
* [FontAwesome](https://fortawesome.github.io/Font-Awesome/)
* [Bootstrap v4.2.1](http://getbootstrap.com/)
* [FontAwesome 4.7](https://fortawesome.github.io/Font-Awesome/)
* [ShipIt](https://github.com/shipitjs/shipit)

I'm also including goodies from:
Expand Down Expand Up @@ -47,3 +47,45 @@ 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.
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,24 @@ app.use(hbs.middleware({


// Error handling middleware
app.use(async(ctx,next) => {
app.use(async(ctx, next) => {
try {
await next();
if (ctx.state.api === true && ctx.status === 200) {
ctx.body = {
error: false,
result: ctx.body
};
}
} catch (err) {
ctx.app.emit("error", err, this);
ctx.status = err.status || 500;
if (ctx.state.api === true) {
return ctx.body = {
error: true,
message: err.message
};
}
await ctx.render("error", {
message: err.message,
error: {}
Expand Down
13 changes: 9 additions & 4 deletions views/layouts/main.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
<link href="/assets/css/bootstrap.min.css" rel="stylesheet">
<link href="/assets/css/font-awesome.min.css" rel="stylesheet">
<link href="/assets/css/bootstrap-social.css" rel="stylesheet">

{{#each vendor_css}}
<link href="{{this}}" rel="stylesheet">
{{/each}}
<!-- Custom styles for this template -->
<link href="/assets/css/custom.css" rel="stylesheet">

Expand Down Expand Up @@ -44,8 +46,11 @@
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
<script src="/assets/js/bootstrap.min.js"></script>
{{#if script}}
<script src="/assets/js/{{script}}.js"></script>
{{/if}}
{{#each vendor_js}}
<script src="{{this}}"></script>
{{/each}}
{{#each scripts}}
<script src="/assets/js/{{this}}.js"></script>
{{/each}}
</body>
</html>

0 comments on commit 6cb41b5

Please sign in to comment.