HAPI的NodeJS单页 [英] nodejs hapi single page

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

问题描述

我有一个单一的应用程序的网站(的NodeJS),我想从防爆preSS为哈啤,我一般是提供静态文件和路由一切到包含angularjs应用和角单页迁移路由配置。

  //防爆preSS路由,首先是静态文件
app.use(如press.static(__目录名称+'/公'));//第二个API路线
app.get('/ API,功能(REQ,RES){
    res.send({API:'响应'})
});//最后一切映射到单页的应用程序:
app.get('*',函数(REQ,RES){
    res.sendfile('./公共/ HTML / controllers.index.html')
});

在HapiJS我不知道如何来复制相同的code(不使用前press.static中间件),这是因为:

 哈皮=要求(高致病性禽流感);
VAR服务器=新Hapi.Server('本地主机',84);server.route({
    方法:GET,
    路径:'/ {P *}
    处理:函数(请求,回复){
        reply.file(公共/ HTML / index.html的');
    }
});

在上面的code,每一个请求无论什么将被映射到我的单页(公共/ HTML / index.html的),但如果我这样做的话,JS,CSS,JPG和放大器;文件将被映射到相同的文件,而不是向脚本,样式和图像(一个请求为'/images/bg.png'将下载单页而不是图像文件)。

我知道,如果我设置的路径/我的单页,然后'{P *}到{目录:{路径:'/公'}}',那么我会的行为,我所需要的,但那里有一个陷阱,如果有些用户拷贝和粘贴特定的URL(可以说'/帐号/登录),然后按下回车键,这条道路将在HapiJS映射和响应将是'未找到(404) ,角路由将永远无法作出回应。

是否有人知道如何解决此问题?

问题的关键部分是:


  • 使用仅HapiJS(无前preSS或其他中间件)

  • 请不要航线每周角路线(路线只是一切尚未路由到单个页面儿子的角度可以处理路由)


解决方案

不知道这是否会帮助你,但你的出发code是一个有点奇怪的Hapi.js.这是我用它来建立一个简单的hapi.js SPA。

如果你想使用特定的URL,如账户/登录,你必须引导你的路径,特定的部分(路径:'/帐号/登录')

的区别在于,即时指向目录作为一个整体,包括这一事实。不同的文件,您只需回复index.html文件。此次上市的参数让你决定,如果你想显示的目录结构,或不在您的网址。默认值是假的。

此处了解详情:<一href=\"http://hapijs.com/tutorials/serving-files#directory-handler\">http://hapijs.com/tutorials/serving-files#directory-handler

\r
\r

VAR哈皮=要求(高致病性禽流感);\r
VAR路径=要求('路径');\r
\r
VAR服务器=新Hapi.Server(8080,'localhost'的);\r
\r
\r
server.route({\r
    方法:GET,\r
    路径:'/ {路径*}\r
    处理程序:{\r
 \t\t目录: {\r
            路径:'./public',\r
            上市:假的,\r
            指标:真\r
        }\r
    }\r
});\r
\r
\r
\r
server.start(函数(){\r
的console.log('服务器启动');\r
});

\r

\r
\r

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')
});

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');
    }
});

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).

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?

The key part of the question is:

  • Use only HapiJS (no express or other middleware)
  • Don't route every angular route (just route everything else not already routed to single page son angular can handle the routing)

解决方案

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.

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

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.

More info here: 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');
});

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

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