Skip to content

gusnips/koa-error-ejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

koa-error-ejs

Error response middleware for koa supporting:

  • text

  • html

  • json

    Adapted from koa-error to use EJS as view engine

Installation

$ npm install koa-error-ejs --save

and then copy the default error view to your views folder

$ cp node_modules/koa-error-ejs/error.html views/

setup in your application

var app= require('koa')();
var error= require('koa-error-ejs');
app.use(error());

Options

  • view String path to template written with ejs. Defaults to {view.root}/error
  • layout String|Boolean layout to use on error view, or false if none. Defaults to false.
  • custom Object specific view for a status code, for example: {404: 'error/not-found'}. Defaults to {}

Example:

var app= require('koa')();
var error= require('koa-error-ejs');
app.use(error({
  view: 'error/error', //(optional)
  layout: 'layouts/error', //(optional)
  custom: { //(optional)
    401: 'error/login',
    403: 'error/forbidden',
    404: 'error/not-found',
    500: 'error/sorry'
  }
}));

Custom templates

By using the view option you can override the bland default template, with the following available local variables:

  • env
  • ctx
  • request
  • response
  • error
  • stack
  • status
  • code

Here's an example:

<!DOCTYPE html>
<html>
  <head>
    <title>Error - <%=status%></title>
  </head>
  <body>
    <div id="error">
      <h1>Error</h1>
      <p>Looks like something broke!</p>
      <% if(env === 'development'){ %>
      <pre>
        <code>
	  <%-stack%>
        </code>
      </pre>
      <% } %>
    </div>
  </body>
</html>

License

MIT

About

Koa error middleware for EJS views

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published