3.0.1 • Published 10 years ago

dragonnodejs-restful v3.0.1

Weekly downloads
16
License
-
Repository
github
Last release
10 years ago

DragonNode.js RESTFul

Bundle with services to develop RESTFul applications

  • Logging with log4js, additional category "express" for the express log4js connector
  • Initialize Express with "body-parser" for JSON encoded request bodies
  • Initialize MongoDB connection with "mongoskin"
  • Add event emitter to the service container for all application events
  • Session services for login and check the session and route to logout
  • Validate input of the client with the "amanda" JSON schema validator

Installation

  • Add bundle to the "package.json":
{
    "dependencies": {
        "dragonnodejs-restful": "3.*"
    }
}
  • Execute "npm install"
  • Extend the configuration for the different environments, for example "./configs/development.js":
module.exports = {
    modules: {
        npm: {
            'dragonnodejs-restful': {
                log: {
                    appenders: [
                        { type: "console" }
                    ],
                    replaceConsole: true
                },
                app: { port: process.env.PORT || 80 },
                db: {
                    serverURLs: 'mongodb://127.0.0.1/app?auto_reconnect=true',
                    dbOptions: { safe: true }
                },
                event: {},
                session: { secret: 'secret' },
                validate: {
                    objectid: {
                        type: 'string',
                        length: 24,
                        required: true
                    }
                }
            }
        }
    }
};

Services

  • Node.js Libraries: events
  • NPM Libraries: amanda, bodyParser, cookieSession, express, log4js, mongoskin
  • Modules:
    • app: "express" application to define routes and connector functions "bodyParser" and "log4js" express logger
    • db: "mongoskin" database connection to the MongoDB
    • event: Event emitter of Node.js "events" to register observers and emit events
    • log: "log4js" default logger to log messages
    • session: ".check" connector function to validate the session, ".login" creates a session
    • validate: Validate input parameter with "amanda" and the configured schemas

Errorhandling

Cause of the problem the errorhandler must be the last connect functions, the bundle can't provide one. That's the reason why the project itself must define the errorhandler.

  • Add module "/modules/errorhandler.js":
"use strict";

/**
 * Errorhandler for the application server
 * @param moduleconfig
 * @param services
 */
module.exports = function (moduleconfig, services) {
    var app = services.app;

    app.use(function (err, req, res, next) {
        res.send({ err: err.message });
    });
};
  • Extend the configuration for the different environments, for example "./configs/development.js":
module.exports = {
    modules: {
        directory: {
            errorhandler: {}
        }
    }
};