Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible do define a global base path in hapi

I want every hapi route path to start with a prefix (/api/1) without adding it to each route. Is this possible?

The following route should be available with path /api/1/pets and not /pets

const Hapi = require('hapi');
const server = new Hapi.Server();
server.route({
    method: 'GET',
    path: '/pets'
})
like image 560
Bernhard Avatar asked Sep 05 '17 11:09

Bernhard


1 Answers

Seems you can't do it globally for the whole application. But there's a possibility to add prefixes for all the routes defined inside a plugin:

server.register(require('a-plugin'), {
    routes: {
        prefix: '/api/1' 
    }
});

Hope this helps.

Just in case, if you're gonna try to add base path via events for new routes, it's not gonna work.

like image 153
Alexander Petrov Avatar answered Oct 18 '22 10:10

Alexander Petrov