我希望将我的Express应用程序迁移到hapi.js,但是我在处理路由方面遇到了麻烦。我只想有两个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*}
我该如何解决这个问题?