vendredi 27 mars 2015

Hapijs - 404 route VS static files route

I'm trying to migrate my Express app to Hapijs, and I having trouble with my routes. I just want 2 GET : my index '/', and everything that is not '/' to redirect to '/'...


On express I had this :



// static files
app.use(express.static(__dirname + '/public'));

// index route
app.get('/', function (req, res) {
// whatever
}

// everything that is not /
app.get('*', function(req, res) {
res.redirect('/');
});


I have issues with Hapijs to get the same behaviour. My "static road" looks like this :



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


and my "404 road" would be :



server.route({
method: 'GET',
path: '/{path*}',
handler: function (request, reply) {
reply.redirect('/');
}
});


and I get this error :



Error: New route /{path*} conflicts with existing /{path*}


How can I resolve this ? Thanks


Aucun commentaire:

Enregistrer un commentaire