Monday 15 July 2013

node.js - nodejs hapi single page -



node.js - nodejs hapi single page -

i have single app site (nodejs) , want migrate express hapi, serve static files , route else single page contains angularjs app , angular routing configuration.

// express routing, first static files app.use( express.static(__dirname + '/public') ); // sec api routes app.get('/api', function(req, res){ res.send( {api: 'response' } ) }); // else maps single page app: app.get('*', function(req, res){ res.sendfile('./public/html/controllers.index.html') });

in hapijs dont know how replicate 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 code above, every request no matter mapped single page ('public/html/index.html'), if this, js, css, jpg & files mapped same file instead scripts, styles , images (a request '/images/bg.png' download single page instead image file).

i know if set path '/' single page , '{p*}' '{directory: {path: '/public'}}' have behaviour need, theres 1 catch, if user re-create , paste specific url (lets '/account/login') , nail enter, route mapped in hapijs , response 'not found (404)', angular routing never able respond.

does know how solve this?

the key part of question is:

use hapijs (no express or other middleware) don't route every angular route (just route else not routed single page boy angular can handle routing)

not sure if help out starting code bit "odd" hapi.js. utilize set simple hapi.js spa.

if wish utilize specific urls account/login, have direct path specific section.(path: '/account/login')

the difference lies in fact i'm pointing directory whole, incl. different files, , reply index.html file. listing parameter lets decide if want show directory construction or not in urls. default false.

more info here: http://hapijs.com/tutorials/serving-files#directory-handler

class="snippet-code-html lang-html prettyprint-override">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'); });

node.js angularjs routing hapijs

No comments:

Post a Comment