我有一个应用程序站点 (NodeJS),我想从 Express 迁移到 Hapi,我通常做的是提供静态文件并将其他所有内容路由到包含 angularjs 应用程序和 angular 路由配置的单个页面。
// Express routing, first the static files
app.use( express.static(__dirname + '/public') );
// Second the api routes
app.get('/api', function(req, res){
res.send( {api: 'response' } )
});
// Finally everything else maps to the single page app:
app.get('*', function(req, res){
res.sendfile('./public/html/controllers.index.html')
});
在 HapiJS 中,我不知道如何复制相同的代码(不使用 express.static 中间件),因为:
Hapi = require('hapi');
var server = new Hapi.Server('localhost', 84);
server.route({
method: 'GET',
path: '/{p*}',
handler: function (request, reply) {
reply.file('public/html/index.html');
}
});
在上面的代码中,无论什么请求都会映射到我的单个页面('public/html/index.html'),但是如果我这样做,那么 js、css、jpg 和文件将被映射到相同的文件而不是脚本,样式和图像(对'/images/bg.png'的请求将下载单个页面而不是图像文件)。
我知道,如果我将路径 '/' 设置为我的单个页面,然后将 '{p*}' 设置为 '{directory: {path: '/public'}}',那么我将拥有我需要的行为,但是有一个问题,如果某个用户复制并粘贴一个特定的 url(比如说'/account/login')然后按回车,该路由将映射到 HapiJS 并且响应将是“未找到(404)”,角度路由永远无法回应。
有谁知道如何解决这个问题?
问题的关键部分是:
- 仅使用 HapiJS(不使用 express 或其他中间件)
- 不要路由每条角度路由(只需将其他所有尚未路由到单页的东西路由到 Angular 可以处理路由)