maglev

Preconfigured NodeJS framework

Maglev (Preconfigured simple NodeJS framework)

Maglev is a simple pre configured server based on Express web framework, Passport authentication middleware and Mongoose database layer. Maglev supports MVC patterns and RESTful routes.

$ npm install maglev
  • Predefined models and controllers (User, Token, Role, Logic...)
  • Extended routing for REST api based on Express
  • Token and session authentication
  • Role based access system
  • Ready for single page applications
  • Swig template system with custom helpers
var Server = require('maglev');
var Config = require('maglev/config');

var config = {
    server: {
        port: 4000
    },
    facebook: {
        clientID: "Facebook App ID",
        clientSecret: "Facebook App Secret",
        namespace: 'Facebook App Namespace'
    }
};

var server = new Server(new Config(config));
server.start();
  • controllers Contains the controllers that handle requests sent to an application.
  • models Contains the models for accessing and storing data in a database.
  • views Contains the views and layouts that are rendered by an application.
  • public Static files and compiled assets served by the application.

Minimal configuration example

var path = require('path');
var rootPath = path.normalize(__dirname + '/..');

var config = {
    db: {
        uri: 'mongodb://localhost/project-name'
    },
    
    server: {
        root: rootPath
    },

    token: {
        secret: 'your secret string for generating of user tokens used for authentication',
    },

    session: {
        secret: 'your secret string for generating of user sessions used for authentication',
    }
};

Define new model

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

function createSchema() {
    var schema = new Schema({
        city: { type: String, required: true },
        street: { type: String, required: true },
        state: { type: String, required: true }
    });

    return schema;
}

module.exports = function (server) {
    return server.db.model('Address', createSchema());   
};
var controllers = require('../controllers');

var user = controllers.user;
var token = controllers.maglev.token;
var message = controllers.message;


module.exports = function(route) {
    route
        .api()
        .get('/messages', token.ensure, message.get)
        .put('/messages/mark/read/:id', token.ensure, message.markAsRead)
        .put('/messages/mark/unread/:id', token.ensure, message.markAsUnread);
};
var config = {
    db: {
        uri: null,
        prepare: db.prepare
    },

    rbac: {
        prepare: rbac.prepare,
        role: {
            guest: 'guest'
        }
    },

    log: {
        on: process.env.NODE_ENV === 'development',
        showStackError: true,
        format: process.env.NODE_ENV === 'development' ? 'dev' : 'combined',
        options: {
            immediate: false
            //stream: process.stdout
        }
    },
    
    server: {
        prepare: express.prepare,
        build: 1,
        timeout: 30000,
        compress: true,
        root: null,
        host: process.env.HOST || '127.0.0.1',
        port: process.env.PORT || 4000
    },

    powered: {
        enabled: false,
        value: 'Maglev'
    },

    responseTime: {
        enabled: true,
        options: {}
    },

    methodOverride: {
        enabled: true,
        getter: 'X-HTTP-Method-Override',
        options: {}
    },

    bodyParser: [{
        parse: 'urlencoded',
        options: {
            extended: true
        }
    }, {
        parse: 'json',
        options: {}
    }, {
        parse: 'json',
        options: {
            type: 'application/vnd.api+json'
        }
    }],

    cookieParser: {
        //https://www.npmjs.org/package/cookie-parser
        enabled: true,
        secret: null,
        options: {}
    },

    token: {
        secret: null,
        expiration: 60*24*14
    },

    secure: {
        prepare: passport.prepare,
        strategies: [
            passport.localStrategy, 
            passport.bearerStrategy, 
            passport.facebookStrategy, 
            passport.facebookCanvasStrategy,
            passport.anonymousStrategy
        ]
    },

    session: {
        secret: null,
        cookie: {
            maxAge: 14 *24 * 60 * 60 * 1000 //2 weeks
        },
        resave: true,
        saveUninitialized: true
    },

    sessionStore: {
        autoReconnect: true,
        collection: 'sessions'
    },

    view: {
        engine: 'swig'
    },

    route: {
        prepare: route.prepare,
        api: {
            path: '/api'    
        } 
    },

    locale: {
        'default': 'en',
        available: ['en'],
        inUrl: false
    },

    country: {
        'default': null,
        available: [],
        inUrl: false
    },  

    registration: {
        simple: true
    },

    facebook: {
        clientID: null,
        clientSecret: null,
        namespace: null
    },

    css: {
        preprocessor: preprocessor.prepare,
        debug: false,
        compress: true,
        sourceMap: true
    },

    upload: {
        maxFieldsSize: 2000000,
        maxFields: 1000,
        path: null
    },

    createPath: [{
        path: '/logs'
    }, {
        path: '/public/files'
    }],

    page: {
        error: page.error,
        notFound: page.notFound
    }
};

Zlatko Fedor

The MIT License (MIT)

Copyright (c) 2015 Zlatko Fedor zlatkofedor@cherrysro.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.