Reputation: 612
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'
})
Upvotes: 7
Views: 1266
Reputation: 558
The best bet would be to use a constant in the paths -
server.route({
method: 'GET',
path: constants.route.prefix + '/pets')
});
and have the constant defined in a static constants.js file
Upvotes: 2
Reputation: 309
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.
Upvotes: 3
Reputation: 3154
I don't see such option in Hapi docs. Still, I can suggest you a small workaround. Make some function:
function createRoutePath(routePath) {
return `/api/1${routePath}`;
}
And then use it this way:
server.route({
method: 'GET',
path: createRoutePath('/pets')
});
UPDATE: As another workaround, leave all as is, and setup proxy web server. For example nginx.
Upvotes: 1