Skip to content

Instantly share code, notes, and snippets.

@sj82516
Created August 26, 2018 08:55
Show Gist options
  • Save sj82516/ffa9be6fe860b73fb5130d5aa5c9cd1f to your computer and use it in GitHub Desktop.
Save sj82516/ffa9be6fe860b73fb5130d5aa5c9cd1f to your computer and use it in GitHub Desktop.
const Koa = require('koa');
const app = new Koa();
app.use(async (ctx, next) => {
try {
await next();
}catch(error){
console.error(error);
ctx.body = "ServerError";
}
});
app.on('error', (err, ctx) => {
console.log("因為有中間件統一處理了Error,這裡理論上不會觸發")
});
// response
app.use(ctx => {
throw new Error("Error");
});
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment