3.0.0 • Published 7 years ago

hapi-routes-prefixer v3.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

hapi-routes-prefixer

A simple plugin to prefix routes with strings such as '/v1' or '/api'.

Why?

When we want to apply prefix to individual route plugins but not to all of the plugins registered with the server object.

Installation

npm install --save hapi-routes-prefixer 

Usage

After installing register the module as a plugin.

var Hapi = require('hapi');
var server = new Hapi.Server();
var prefixer = require('hapi-routes-prefixer');

server.connection({
	port: 3000,
	host: localhost
});

server.register([
	{
		register: prefixer
	},
	{
		register: 'other plugins'
	}, function (err) {
		if (err) console.log('Failed to load plugins ',err);
		console.log('Server running at:', server.info.uri);
	}
]);

After registering the plugin, use it in your routes like this.

exports.register = function (server, option, next) {

  var route = server.routePrefix('/api');

  route([
    {
      method: 'GET',
      path: '/users/{id}',
      handler: function (request, reply) {
      }
    },
    {
      method: 'POST',
      path: '/users',
      handler: function (request, reply) {
      	reply('created user');
      }
    },
  ]);

  next();
};

exports.register.attributes = {
  name: 'api.user',
  version: '1.0.0'
};

The above routes can be accessed at 'domain.com/api/users' now.