hapi.js - 404 route VS static files route

谁都会走 提交于 2019-12-22 03:23:04

问题


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

Using 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 hapi.js 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?


回答1:


You're defining 2 routes with the same method and path, which is a conflict as far as hapi's router is concerned. That's why you're getting the error.

If a file isn't found by the directory handler, it will respond with a 404 error by default.

What you can do is intercept this with an onPreReponse handler, which checks if the response is an error response (a Boom object), and if so respond however you wish. In your case by redirecting to /:

var Hapi = require('hapi');

var server = new Hapi.Server();
server.connection({ port: 4000 });

server.route([{
        method: 'GET',
        path: '/',
        handler: function (request, reply) {

            reply('Welcome home!');
        }
    }, {
        method: 'GET',
        path: '/{p*}',
        handler: {
            directory: {
                path: 'public',
                listing: false
            }
        }
    }
]);

server.ext('onPreResponse', function (request, reply) {

    if (request.response.isBoom) {
        // Inspect the response here, perhaps see if it's a 404?
        return reply.redirect('/');
    }

    return reply.continue();
});


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

Recommended reading:

  • hapi's request lifecycle and extension points: http://hapijs.com/api#request-lifecycle
  • The response object: http://hapijs.com/api#response-object


来源:https://stackoverflow.com/questions/29305065/hapi-js-404-route-vs-static-files-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!