I have one application site (NodeJS) and I want to switch from Express to Hapi, what I usually do is static files and redirecting everything else to one page containing the angularjs application 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 don't know how to copy 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 above code, each request no matter what will be displayed on my only page ('public / html / index.html'), but if I do, then js, css, jpg and files will be displayed to the same file instead scripts, styles, and images (a request to "/images/bg.png" will load one page instead of an image file).
I know that if I set the path "/" to my only page and then "{p *}" to "{directory: {path: '/ public'}}, then I will have the behavior that I need but theres one catch if any user copies and pastes a specific url (say '/ account / login') and then press enter, this route will be displayed in HapiJS and the answer will be โNot Found (404)โ. angular routing will never be able to respond.
Does anyone know how to solve this?
The key part of the question:
- Use only HapiJS (without special or other middleware).
- Do not route each angular route (just go through everything else that is not redirected to the page yet, the angular page can handle routing)
Amรญlcar calles
source share