小编Thi*_*lon的帖子

hapi.js - 404路由VS静态文件路由

我正在尝试将我的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('/');
});
Run Code Online (Sandbox Code Playgroud)

我有hapi.js的问题,以获得相同的行为.我的"静态路"看起来像这样:

server.route({
  method: 'GET',
  path: '/{path*}',
  handler: {
    directory: {
      path: 'public',
      listing: false
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

而我的"404道路"将是:

server.route({ 
  method: 'GET', 
  path: '/{path*}', 
  handler: function (request, reply) {
    reply.redirect('/');
  }
});
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Error: New route /{path*} conflicts with existing /{path*}
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

redirect node.js http-status-code-404 hapijs

11
推荐指数
1
解决办法
3503
查看次数

标签 统计

hapijs ×1

http-status-code-404 ×1

node.js ×1

redirect ×1