【发布时间】:2023-10-28 22:36:01
【问题描述】:
我正在尝试将我的 Express 应用程序迁移到 hapi.js,但我的路线遇到了问题。我只想要 2 GET:我的索引 '/',以及所有不是 '/' 的东西都重定向到 '/'。
使用 Express 我有这个:
// static files
app.use(express.static(__dirname + '/public'));
// index route
app.get('/', function (req, res) {
// whatever
}
// everything that is not /
app.get('*', function(req, res) {
res.redirect('/');
});
我在使用 hapi.js 时遇到了问题,无法获得相同的行为。我的“静态道路”是这样的:
server.route({
method: 'GET',
path: '/{path*}',
handler: {
directory: {
path: 'public',
listing: false
}
}
});
而我的“404 路”将是:
server.route({
method: 'GET',
path: '/{path*}',
handler: function (request, reply) {
reply.redirect('/');
}
});
我得到这个错误:
Error: New route /{path*} conflicts with existing /{path*}
我该如何解决这个问题?
【问题讨论】:
标签: node.js redirect http-status-code-404 hapijs