nodejs hapi 单页 [英] nodejs hapi single page

查看:34
本文介绍了nodejs hapi 单页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序站点 (NodeJS),我想从 Express 迁移到 Hapi,我通常做的是提供静态文件并将其他所有内容路由到包含 angularjs 应用程序和 angular 路由配置的单个页面.

I have a single app site (NodeJS) and I want to migrate from Express to Hapi, what I normally do is serve static files and route everything else to a single page which contains the angularjs app and the angular routing configuration.

// 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 中间件),因为:

In HapiJS I dont know how to replicate the same code (without using express.static middleware), because:

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"的请求将下载单个页面而不是图像文件).

In the code above, every request no matter what will be mapped to my single page ('public/html/index.html'), but if I do this, then the js, css, jpg & files will be mapped to the same file instead to the scripts, styles and images (a request to '/images/bg.png' will download the single page instead the image file).

我知道,如果我将路径 '/' 设置为我的单页,然后将 '{p*}' 设置为 '{directory: {path: '/public'}}' 那么我将有我的行为需要,但有一个问题,如果某些用户复制并粘贴特定的 url(比如/account/login")然后按回车键,该路由将被映射到 HapiJS 中,并且响应将是未找到(404)", Angular 路由将永远无法响应.

I know that if I set up the path '/' to my single page and then '{p*}' to '{directory: {path: '/public'}}' then I will have the behaviour that I need, but theres one catch, if some user copy and paste an specific url (lets say '/account/login') and then hit enter, that route will be mapped in HapiJS and the response will be 'Not Found (404)', angular routing will never be able to respond.

有人知道如何解决这个问题吗?

Does anybody know how to solve this?

问题的关键部分是:

  • 仅使用 HapiJS(无 express 或其他中间件)
  • 不要路由每条 angular 路由(只需路由所有其他尚未路由到单页的 Angular 可以处理路由)

推荐答案

不确定这是否会对您有所帮助,但您的起始代码对于 Hapi.js 来说有点奇怪".这是我用来设置一个简单的 hapi.js SPA.

Not sure if this will help you out but your starting code is a bit "odd" for Hapi.js. This is what i use to set up a simple hapi.js SPA.

如果您希望使用特定的 URL,例如帐户/登录名,您必须将您的路径定向到该特定部分.(path: '/account/login')

If you wish to use specific URLS like the Account/Login, you have to direct your path to that specific section.(path: '/account/login')

不同之处在于我指向整个目录,包括.不同的文件,您只需回复 index.html 文件.列表参数让您决定是否要在您的 url 中显示目录结构.默认为 false.

The difference lies in the fact that i'm pointing to the directory as a whole, incl. the different files, and you simply reply the index.html file. The listing parameter lets you decide if you want to show directory structure or not in your urls. Default is false.

更多信息在这里:http://hapijs.com/tutorials/serving-files#directory-handler

var Hapi = require('hapi');
var Path = require('path');

var server = new Hapi.Server(8080, 'localhost');


server.route({
    method: 'GET',
    path: '/{path*}',
    handler: {
 		directory: {
            path: './public',
            listing: false,
            index: true
        }
    }      
});



server.start(function(){
	console.log('server started');
});

这篇关于nodejs hapi 单页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆