Ai
3 Star 0 Fork 0

mirrors_HttpErrorPages/HttpErrorPages

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
koa.js 2.05 KB
一键复制 编辑 原始数据 按行查看 历史
Andi Dittrich 提交于 2020-11-30 03:17 +08:00 . expose response object within filter callback
const _koa = require('koa');
const _webapp = new _koa();
// use require('http-error-pages') for regular apps!
const _httpErrorPages = require('../lib/main');
async function bootstrap(){
// use http error pages handler (INITIAL statement!)
// because of the asynchronous file-loaders, wait until it has been executed - it returns an async handler
_webapp.use(await _httpErrorPages.koa({
lang: 'en_US',
payload: {
footer: 'Hello <strong>World</strong>',
pagetitle: 'we are sorry - an internal error encountered',
},
filter: function(data, ctx){
// remove footer
//data.footer = null;
return data;
},
onError: function(data){
// for debugging purpose only!!
// use custom middleware for errorlogging!!
console.log(`[koa] ERROR: ${data.title}\n${data.error.stack}`)
}
}));
// demo handler
_webapp.use(async (ctx, next) => {
if (ctx.path == '/'){
ctx.type = 'text';
ctx.body = 'HttpErrorPages Demo';
}else{
return next();
}
});
// throw an 403 error
_webapp.use(async (ctx, next) => {
if (ctx.path == '/my403error'){
ctx.throw(403);
}else{
return next();
}
});
// throw an 533 error with status 500
_webapp.use(async (ctx, next) => {
if (ctx.path == '/x533'){
const e = new Error("custom");
e.errorpage = '533';
e.status = 500;
throw e;
}else{
return next();
}
});
// throw an internal error
_webapp.use(async (ctx, next) => {
if (ctx.path == '/500'){
throw new Error('Server Error');
}else{
return next();
}
});
// start service
_webapp.listen(8888);
}
// invoke bootstrap operation
bootstrap()
.then(function(){
console.log('Running Demo on Port 8888');
})
.catch(function(e){
console.error(e);
});
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_HttpErrorPages/HttpErrorPages.git
git@gitee.com:mirrors_HttpErrorPages/HttpErrorPages.git
mirrors_HttpErrorPages
HttpErrorPages
HttpErrorPages
master

搜索帮助