ever-js

1.1.3 • Public • Published
powered by

Gitter Issues bitHound Score Code Climate

Enjoyment Via Easiness of Restify - js

npm package


This framework is completely based on Restify.



Installing the framework

Please note that, -g is required to use.

npm install -g ever-js

Building the first app

Follow the below mentioned steps to create the first application.

mkdir MyFirstApp
cd MyFirstApp/

Then run following command to initialise framework.

everjs init

Folder structure

MyFirstApp/
    |-routes/
     |--Routes.js
    |-lib/
        |--SampleLib.js
    |-configuration/
        |-global/
            |--common.json
            |--default.json
            |--production.json
        |--RestifyConfig.js
    |-handlers/
        |--MongoHandler.js
    |-filters/
        |--SampleFilters.js
    |-middleware/
        |--SampleMw.js
    |-kernel/
        |--Banner.js  
        |--Kernel.js  
        |--Server.js
    |--main.js
    |--package.json

Global Variables


Variables Usage
_ This is Lodash module.
eg:
_.forEach
GlobalConfig Configureation values based on the environment(default.json or production.json)
and common.json

To access environment based setting values.
GlobalConfig.[setting value]
eg :
GlobalConfig.db.mongo

To access Common Values in commong.js
GlobalConfig.Common.[setting value]
eg:
GlobalConfig.Common.db_handlers
Dbh Contains the Database handlers.
Eg: Dbh.Mongo
Lib Contains modules defined in lib/ folder.
Eg: Lib.SampleLib.sampleFunction();
(not avaialbe to use inside lib/ )

handlers/


This contains all the handler modules.


MongoHandler.js

This handler manage Mongo Database connections.

Usage of Mongo Hanlder.

sampleFunction : function() {
    Dbh.Mongo.connect(function(err, db){
     db.collection("Test").find({}).toArray(function(err, result) {
         console.log(result);
     });
    });
}

routes/


This contains all the routing information. These settings are stored in Routes.js

Structure of the Routes.js as follows.

module.exports = {
 
    getRoutes : function(dependencies) {
 
        var userFilters = dependencies.UserFilters;
        var userMiddleware = dependencies.userMiddleWare;
 
        return {            
            GET: [],
            POST: [],
            PUT: [],
            DELETE: []
            }
    }
}
  • GET holds all the routings uses http GET method

  • POST holds all the routing uses http POST method

  • PUT holds all the routing uses http PUT method

  • DELETE holds all the routing uses http DELETE method

  • Adding a new route Following is the syntax for adding a route.

HTTP_METHOD : [
    {
        config: {
         path: 'Patter of the route',
         isSecure: true | false 
        },
        method: userMiddleware.[Middleware Name].[Method Name],
        filters: [filter 1, filter 2, ...]
    }
]
  • config
  • path - Pattern of the route

  • isSecure - If set true, a authentication function must be set in the config section as mentioned below section.

  • method - Holds middleware to execute on particular pattern

  • filters - Executes before the Middleware defined in the method section.

The sample Route file is as follows.

module.exports = {
 
    getRoutes : function(dependencies) {
        var userFilters = dependencies.UserFilters;
        var userMiddleware = dependencies.userMiddleWare;
        return {
             GET: [
                 {
                     config: {
                         path: '/get-sample-route-no-filter',
                         isSecure: false
                     },
                     method: userMiddleware.SampleMw.getWithoutFilters,
                     filters: []
                 },
                 {
                     config: {
                         path: '/get-sample-route-with-filter',
                         isSecure: true
                     },
                     method: userMiddleware.SampleMw.getWithFilters,
                     filters: [userFilters.SampleFilters.sampleFilter]
                 }
             ],
             POST: [],
             PUT: [],
             DELETE: []
            }
    }
}

lib/


Please refer the sample filter file

Node modules added to this folder can be access via User Middleware and filters.

Syntax for accessing the Lib files.

Lib.[Module file name].[function name]()

Example:

sampleFunction() in SampleLib.js module accessible as follows.

Lib.SampleLib.sampleFunction();

configuration//


This folder contains the main config file RestifyConfig.js

  • AuthFunction section
  AuthFunction : function(dependencies){

In this section, if you do not have any authentication filter, use as follows.

    return false;

Else, follow below mentioned syntax,

    return dependencies.filters.[Filter name].[Authentication function name] ;

example:

   return dependencies.filters.SampleFilters.sampleAuthFilter;
  • AppConfig section

This section is dedicate to Restify Create Server paramters

  AppConfig : function(dependencies){

You can add create server parameters as follows,

{
  certificate: ...,
  key: ...,
}

This section has the

  • Application name retrieves from package.json
  • Server Port, which is default to 8312 or can be set using an environment variable : RESTIFY_PORT
  • Listen address, which is to all the connections or can be set using an environment variable: RESTIFY_ LISTEN_IP
  name: dependencies.packageJson.name,
  port : 8312 || process.env.RESTIFY_PORT,
  address : "0.0.0.0" || process.env.RESTIFY_LISTEN_IP
  • RestifyMiddlewareSet section

This section is used to load the Restify plugins

  RestifyMiddlewareSet : function(dependencies) {

Following plugins are loaded as default settings

  dependencies.restifyObject.acceptParser(dependencies.serverObject.acceptable),
  dependencies.restifyObject.gzipResponse(),
  dependencies.restifyObject.queryParser(),
  dependencies.restifyObject.bodyParser()
  • RestifyPre section

This section is used to load Restify's pre() modules.

  RestifyPre : function(dependencies) {

Use the following syntax to load the load the modules.

dependencies.restifyObject.[pre filter|pre function|pre module]

Example :

return [
          dependencies.restifyObject.pre.sanitizePath(),
          dependencies.restifyObject.pause()
    ];

configuration/global/

Contains all the configuration values based environment. If NODE_ENV is NOT defined, default.json is loaded. for Production configuration values.

export NODE_ENV=production
File name Descriptions Environment
default.json contains none production environment values.
default environment.
production.json contains production enviroment values. Production
common.json Contains common values for both the enviroments All

default.json

{
    "environment" : "default",
    "connectionStrings" : {
        "mongo" : "mongodb://127.0.0.1/test"
    }
}

production.json

{
    "environment" : "production",
    "db" : {
        "mongo" : "mongodb://127.0.0.1/test"
    }
}

common.json

{
    "port" : "8312",
    "address" : "0.0.0.0",
    "db_handlers" : {
        "Mongo" : {
            "enable" : true,
            "libPath" : "../handlers/MongoHandler.js"
        }
    }
}

db_handlers contains the information of each Database handler. libPath must be defined relative to the kernel/


filters/


Please refer the sample filter file

Contains the functionality which executes prior to assigned user middleware.

Node modules added to this folder can be access from Routes.js.

Format of the filter function is very important !!!

Please follow below mentioned format for filter function.

function(req, res, next) {
        //Your logic goes here
        next();
}

sample filter :

sampleFilter : function(req,res,next) {
        console.log("Sample Filter.");
        next();
}

Syntax for accessing the filter files from Routes.js.

userFilters.[Filter file name].[filter function name]

Example:

sampleFilter() in SampleFilters.js module accessible as follows.

userFilters.SampleFilters.sampleFilter();

middleware/


Please refer the sample filter file

Contains the functionality which executes based on the route patterns.

Node modules added to this folder can be access from Routes.js.

Format of the Middleware function is very important !!!

Please follow below mentioned format for filter function.

function(req, res, next) {
        //Your logic goes here
}

sample Middleware :

getWithoutFilters: function (req, res, next) {
   res.send(200, {msg: "GET Without Filters"});
},

Syntax for accessing the filter files from Routes.js.

userMiddleware.[Middleware file name].[Middleware function name]

Example:

getWithoutFilters() in SampleMw.js module accessible as follows.

userFilters.SampleMw.getWithoutFilters;

kernel/


Contains the heart of the framework. Try not to edit this.

But, be my guest to suggest any modification.

  • Kernel.js - Loads all the dependencies and pass them to modules.
  • Server.js - This is where Restify server loads all the routes, lib, middleware, filter and configs.
  • Banner.js - Just prints my logo :)

main.js


Executes the kernel of the framework. Not much to see.


package.json


Default package.json for the current application. You can add any dependencies which required. But, please do not remove any existing ones.

Package Sidebar

Install

npm i ever-js

Weekly Downloads

1

Version

1.1.3

License

MIT

Last publish

Collaborators

  • geeg