Skip to content

Add REST HTTP methods directly to server object of hapi.JS framework to easily add routes

License

Notifications You must be signed in to change notification settings

daraosn/hapi-rest-methods

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hapi-rest-methods

Add REST HTTP methods directly to server object of hapi.JS framework to easily add routes.

Build Status

Usage

Coming from ExpressJS and trying not to look back into it, I was introduced to HapiJS, which is a nice alternative, but routes definition is an overkill, I like simplicity of ExpressJS, so I hooked some handy methods to the hapi server object.

Instead of doing:

server.route({
  type: 'GET',
  path: '/foo',
  handler: function(request, reply) {
    reply('bar');
  });
});

You can just do:

server.get('/foo', function(request, reply) {
  reply('bar');
});

Simple.

It also supports .post(), .put(), .patch(), .delete() and .options(). Use .any() to match all of the (alias for * method).

Usage

Simple usage:

  // new hapi server
  var hapi = require('hapi');
  var restMethods = require('../')
  var server = new hapi.Server();
  server.connection({ port: 8080 });
  // add hapi-rest-methods plugin
  server.register(restMethods);
  // keep. it. simple. stupid. :-)
  server.get('/fruit', function(request, reply) {
    reply('orange');
  });
  server.post('/grumpy', function(request, reply) {
    console.log(request.payload.name)
    reply('cat');
  });

Plays well with other plugins, such as hapi inert:

  var inert = require('inert');
  ...
  server.register(restMethods);
  server.register(inert);
  server.get('/', { file: 'public/index.html' });

And if you need to config routes, just pass 3 parameters (path, config, handler):

  var routeConfig = config: {
    description: 'Say hello!',
    notes: 'The user parameter defaults to \'stranger\' if unspecified',
    tags: ['api', 'greeting'],
    auth: { ... },
    cache: { ... }
    // ...
  }

  server.delete('/the-internet', routeConfig, function(request, reply) {
    reply('...');
  });

Issues & Contributing

Use github issues.

License

MIT

About

Add REST HTTP methods directly to server object of hapi.JS framework to easily add routes

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published