1.0.0 • Published 9 years ago

hapi-rest-routes v1.0.0

Weekly downloads
1
License
GNU General Publi...
Repository
github
Last release
9 years ago

hapi-rest-routes

Hapi extension to generate restful routes. It is inspired by the module hapi-resource of Sako Hartounian https://github.com/sakoh/hapi-resource

Install

$ npm install --save hapi-rest-routes

Usage

Given the fact that you have a restful resource controller for your blog posts with a index and show function.

var PostController = {

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

};

To generate the routes for your controller, you just have to use the hapit-rest-routes package.

var hapiRestRoutes = require('hapi-rest-routes');
var PostController = require('./PostController');

var restRoutes = hapiRestRoutes({
    name: 'post',
    namespace: 'blog',
    controller: PostController,
    methods: ['index', 'show']
});

This will generate the following routes.

[
    {
        method: "GET",
        path: "/blog/posts",
        controller: PostController.index,
        config: {
            id: "blog.posts.index"
        }
    },
    {
        method: "GET",
        path: "/blog/posts/{id)",
        controller: PostController.show,
        config: {
            id: "blog.posts.show"
        }
    }
]

Options

options.name (required)

Type: String

Name of the resource.

options.namePlural optional

Type: String Default Value: options.name + 's'

If you have a resource that needs a specific plural name for the url and route id, you're able to specify this name with this attribute. The default value will be generated by the letter s added to the name attribute.

options.controller (required)

Type: Object

Your Controller object including all controller functions you need.

options.namespace optional

Type: String

Structure your resources within namespaces to support multiple resources with the same name. In example you're able to add a post controller for blog posts and board posts by specifying a blog and board namespace.

options.methods optional

Type: Array Default Value: ['index', 'show', 'create', 'store', 'edit', 'update', 'destroy']

Limit the restful functions you want to support for your resource. If this attribute isn't set all restful functions will be supported.

options.variable optional

Type: String

Regular expression to limit the value of the id parameter to match this expression.

License

GNU General Public License v2.0 © Dominic Bäuerle