koa-2-error-handler
v0.0.6
Published
Error handler for Koa 2 application.
Downloads
9
Maintainers
Readme
Koa.js 2 error handler
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",
...
}