0.0.21 • Published 10 years ago

kingslanding v0.0.21

Weekly downloads
2
License
MIT
Repository
github
Last release
10 years ago

Mongoose Restify Server

A quick API server setup using mongoose and restify. Configuration files are used to setup model, routes and restful services.

Installation

npm install --save kingslanding

Test

npm test

Usage

index.js

var server = require('kingslanding');
server.lift(config, function() {
    console.log('Server lifted.');
});

where the configuration settings config can be defined either in a variable or in separate config.js as

module.exports = {
    port: 8085,
    mongo: 'mongodb://localhost/test',
    model: __dirname,
    controller: __dirname,
    routes: {
        blog: {}
    }
}

Here the blog is defined using Mongoose model in blog.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Mixed = Schema.Types.Mixed;
module.exports = {
    fields: {
        title: { type: String },
    },
    options: {
        collection: 'blog',
        versionKey: false,
        timestamps: {},
        runValidators: false
    },
    methods: {},
    indexes: {},
    virtuals: {}
}

Since we use the module to do all the heavy lifting for us, we can just leave empty in the blogController.js

module.exports = {
};

That's it, if you fire the server, you should see a restful resources for blog on port 8085.

More

The routes in the config.js can have non-restful calls,

var config = {
    port: 8085,
    routes: {
        /**
         * GET call
         */
        get: {
            path: '/get',
            GET: httpCall,
        },
        /**
         * POST call
         */
        post: {
            path: '/post',
            POST: httpCall,
        },
        /**
         * Bundle call
         */
        bundle: {
            path: '/bundle',
            GET: httpCall,
            POST: httpCall,
        },
        /**
         * REST call
         */
        rest: {
            path: '/rest',
            REST: {
                query: httpCall,
                detail: httpCall,
                insert: httpCall,
                patch: httpCall,
                del: httpCall
            }
        },
        /**
         * Group call
         */
        group: {
            path: '/group',
            items: {
                get: {
                    path: '/get',
                    GET: httpCall,
                },
                post: {
                    path: '/post',
                    POST: httpCall,
                },
                bundle: {
                    path: '/bundle',
                    GET: httpCall,
                    POST: httpCall,
                },
                rest: {
                    path: '/rest',
                    REST: {
                        query: httpCall,
                    }
                }
            }
        },
        /**
         * Mixed call
         */
        mixed: {
            path: '/mixed',
            items: {
                get: {
                    path: '/get',
                    GET: httpCall
                }
            },
            REST: {
                query: httpCall,
                insert: httpCall,
            }
        }
    }
};

In that case, you do not need to specify the mongo, model and controller, because each httpCall can be a simple express route function

var httpCall = function(req, res, next) {
    res.send('Hello server');
    next();
};

In case you want to organize routes using controller actions, you can do

var config = {
    port: 8085,
    controller: '../../test/fixture',
    routes: {
        /**
         * GET call with controller
         */
        ctrl: {
            path: '/ctrl',
            controller: 'index',
            GET: 'index',
        },
        /**
         * REST call with controller
         */
        rest: {
            path: '/rest',
            controller: 'rest',
            REST: ['query', 'detail']
        },
        /**
         * Group call with controller
         */
        group: {
            path: '/group',
            controller: 'index',
            items: {
                'index': {
                    path: '',
                    GET: 'index'
                },
                'debug': {
                    path: '/debug',
                    POST: 'debug'
                },
                'rest': {
                    path: '/rest',
                    REST: ['query', 'detail']
                }
            }
        },
    }
};

where the method value (ex. GET) is replaced by the action name if controller is specified.

0.0.21

10 years ago

0.0.19

10 years ago

0.0.18

10 years ago

0.0.17

10 years ago

0.0.16

10 years ago

0.0.15

10 years ago

0.0.14

10 years ago

0.0.13

10 years ago

0.0.12

10 years ago

0.0.11

10 years ago

0.0.10

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago