Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript. Learn more »

koa-2-error-handler

0.0.6 • Public • Published

Koa.js 2 error handler

Build Status Coverage Status

Description

Error handler for Koa 2 application

Installation

npm i -S koa-2-error-handler

Documentation

Error handle middleware

Error handle middleware should be the first app.use middleware

const Koa = require('koa');
const app = new Koa();
const error = require('koa-2-error-handler').error;
app.use(error());

Or we can update response error message:

app.use(error((err, ctx) => {
    ctx.body = {
        message: err.message
    };
    console.error(err);
}));

Server event listeners

We can use node.js http server and process server events:

app.js:

const Koa = require('koa');
const app = module.exports = new Koa();

bin/www

#!/usr/bin/env node

/**
 * Module dependencies.
 */
const http = require('http');
const errorHandler = require('koa-2-error-handler');
const app = require('../app');

/**
 * Create HTTP server.
 */
const server = http.createServer(app.callback());

/**
 * Listen on provided port, on all network interfaces.
 */
server.listen(errorHandler.helper(process.env.PORT || '3000'));

/**
 * Handling  listening and error events
 */
errorHandler.server(server, console.info, console.error);

package.json:

"scripts": {
    "start": "node --harmony ./bin/www",
    ...
}

Packages

Modules:

Testing:

install

npm i koa-2-error-handler

Downloadsweekly downloads

14

version

0.0.6

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability