Hapijs自定义500错误页面 [英] Hapijs Custom 500 Error Page
问题描述
查看Hapi的文档并尝试使用google,我可以找到如何设置404页面的方法,但是找不到关于设置500页的任何信息.
我尝试添加如下错误处理程序:
server.on('internalError', function (request, err) {
console.log("Internal Error:", err);
request.reply.view('errors/500', {
error: err
}).code(500);
});
但是我的钩子从未被调用过.有没有一种简单的方法可以使用Hapijs返回自定义的500页?
您需要将错误响应捕获在onPreResponse
扩展函数中,并在其中设置新的HTML响应.
相同的原理适用于任何Boom错误,无论是您在处理程序中设置的错误,还是内部由hapi设置的错误(例如,404未找到或401未经授权的未授权用户.
以下是您可以尝试的示例:
index.js
const Hapi = require('hapi');
const Path = require('path');
const server = new Hapi.Server();
server.connection({ port: 4000 });
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply(new Error('I\'ll be a 500'));
}
});
server.ext('onPreResponse', (request, reply) => {
if (request.response.isBoom) {
const err = request.response;
const errName = err.output.payload.error;
const statusCode = err.output.payload.statusCode;
return reply.view('error', {
statusCode: statusCode,
errName: errName
})
.code(statusCode);
}
reply.continue();
});
server.register(require('vision'), (err) => {
if (err) {
throw err;
}
server.views({
engines: {
hbs: require('handlebars')
},
path: Path.join(__dirname, 'templates')
});
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});
});
templates/error.hbs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{title}}</title>
<style>
body {
text-align: center;
background: #B0B0B0;
color: #222;
}
.error h1 {
font-size: 80px;
margin-bottom: 0;
}
</style>
</head>
<body>
<div class="error">
<h1>⚠<br/>{{statusCode}}</h1>
<h2>{{errName}}</h2>
</div>
</body>
</html>
通过转到 http://localhost:4000/进行测试,以查看您的自定义错误页面:>
这种方法可以捕获任何Boom响应,包括由hapi而非我们内部生成的响应.因此,也可以解决4xx错误.尝试导航到 http://localhost:4000/notapage ,您会得到相同的漂亮页面,但其中包含404 :
Looking through Hapi's documentation, and trying to google, I can find how to setup a 404 page, but I cannot find anything about setting up a 500 page.
I tried adding an error handler like follows:
server.on('internalError', function (request, err) {
console.log("Internal Error:", err);
request.reply.view('errors/500', {
error: err
}).code(500);
});
But my hook never gets called. Is there an easy way to return a custom 500 page with Hapijs?
You need to trap the error response inside an onPreResponse
extension function and set a new HTML response there.
The same principle applies to any Boom error, whether it be one set by you in a handler or set by hapi internally (e.g. a 404 Not found or a 401 Unauthorized from failed auth.
Here's an example that you can try yourself:
index.js
const Hapi = require('hapi');
const Path = require('path');
const server = new Hapi.Server();
server.connection({ port: 4000 });
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply(new Error('I\'ll be a 500'));
}
});
server.ext('onPreResponse', (request, reply) => {
if (request.response.isBoom) {
const err = request.response;
const errName = err.output.payload.error;
const statusCode = err.output.payload.statusCode;
return reply.view('error', {
statusCode: statusCode,
errName: errName
})
.code(statusCode);
}
reply.continue();
});
server.register(require('vision'), (err) => {
if (err) {
throw err;
}
server.views({
engines: {
hbs: require('handlebars')
},
path: Path.join(__dirname, 'templates')
});
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});
});
templates/error.hbs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{title}}</title>
<style>
body {
text-align: center;
background: #B0B0B0;
color: #222;
}
.error h1 {
font-size: 80px;
margin-bottom: 0;
}
</style>
</head>
<body>
<div class="error">
<h1>⚠<br/>{{statusCode}}</h1>
<h2>{{errName}}</h2>
</div>
</body>
</html>
Test it out by going to http://localhost:4000/ to see your custom error page:
This approach catches any Boom response, including those generated internally by hapi rather than by us. Therefore will also work with 4xx errors. Try navigating to http://localhost:4000/notapage and you'll get the same pretty page but for a 404:
这篇关于Hapijs自定义500错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!