Skip to content

This is a resource function created for hapi.js in order to in order to reduce the amount of boilerplate code when writing hapi routes

License

Notifications You must be signed in to change notification settings

zainul/hapi-resource-z

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hapi-resource

This is a resource function created for hapi.js to reduce the amount of code when writing hapi routes. this package include plural (s/ies) feature

###INSTALLATION

npm install hapi-resource-z

##USAGE

Given an API controller:

const joi             = require('joi');
var PostsController = {

  index: function(request, reply) {
   ....
  },

  show: function(request, reply) {
    .....
  },

  create: function(request, reply) {
    .....
  },  
}

you can now write:

server.route(
  resource({
    name: 'post',
    controller: PostsController,
    validate: payload: {
      name: joi.string().alphanum().required(),
    },
    methods: ['all', 'show', 'create', 'update', 'delete' ] // optional if not defined (by default * included)
  })
);

Instead of writing:

hapiServer.route([
  {
    method : "GET",
    path : "/posts",
    handler : PostsController.index
  },
  {
    method : "GET",
    path : "/posts/{id}",
    handler : PostsController.show
  },
  ...
]);

You can also easily namespace your routes:

var resource = require('hapi-resource-z');

server.route(
  resource({
    name: 'user',
    controller: UsersController,
    namespace: '/admin'
  })
);

is equivalent to:

hapiServer.route([
  {
    method : "GET",
    path : "/admin/users",
    handler : UsersController.index
  },
  {
    method : "GET",
    path : "/admin/users/{id}",
    handler : UsersController.show
  },
  ...
]);

About

This is a resource function created for hapi.js in order to in order to reduce the amount of boilerplate code when writing hapi routes

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published