Hapi: prefix all routes

Created on 30 Aug 2016  路  4Comments  路  Source: hapijs/hapi

Hi. i would like to add a prefix to every of my url.
I'm aware of http://hapijs.com/api#serverrealm i'm sure i can use it for that.

so i'm considering using this approach:

routes.forEach(route => {
  if (PREFIX_URL) route.path = `${PREFIX_URL}${route.path}`
  Server.route(route)
})

maybe you could advice a better approach ?

support

All 4 comments

You can use the prefix setting in the registration plugin level like the example follow, when you use the options routes.prefix in registring a plugin all your routes inside the plugin will have the prefix.

server.register({ register: require('myplugin') }, {
    routes: {
        prefix: '/plugins'
    }
}, (err) => {

    if (err) {
        throw err;
    }
});

Reference: http://hapijs.com/tutorials/plugins#plugin-options

Please read the issues instruction and post support questions in hapijs/discuss.

no longer working for hapi v17?

Edit:

It looks like Hapi Glue changed the configuration a bit, it no longer use options attribute to set the plugin options.

register: {
    plugins: [ { plugin: 'myplugin', routes: { prefix: '/test/' } } ]
  } 

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

Was this page helpful?
0 / 5 - 0 ratings