nmvc

0.0.15 • Public • Published

NMVC

Simple ES6 MVC framework built on Express.

An example project can be found here: https://github.com/TJMoats/nmvc_demo

Installation

$ npm install nmvc
 
$ node app.js --harmony_default_parameters

Simple run

var NMVC = require('nmvc');
nmvc = new NMVC();
nmvc.run();

Default settings

NMVC runs in dev mode be default, using /config/dev.json as it's default config file. You can change what mode you run in by changing the env variable.

//Use config/production.json as the config file.
nmvc.env = 'production'

By default NMVC runs with the following directory layout

  • Root
    • config
      • dev.json
    • helpers
    • controllers
    • models
    • views
    • assets
      • public

You can modify the directories used like so:

nmvc = new NMVC();
 
nmvc.config_dir = 'relative/directory/';
nmvc.helper_dir = 'relative/directory/';
nmvc.model_dir = 'relative/directory/';
nmvc.controller_dir = 'relative/directory/';
nmvc.views_dir = 'relative/directory/';
nmvc.public_dir = 'relative/directory/'
 
nmvc.run();

Express

This code acts as a simple organizer for an express based application. Anything that works in express will work with this lib. You can access the express object like this:

nmvc.app

Controllers

All controllers extend the base nmvc base controller. You can create your own base controller and extend that as well.

"use strict";
module.exports = class AdminController extends nmvc.base_controller {
    constructor() {
        super();
        this.prefix = '/admin';
        this.enabled = true;
    }
 
    index(cb) {
        
    }
    
    //This function will not create a route.
    _doStuff() {
    
    }
};

There are two variables that you need to exist in the controller for it to function correctly: prefix and enabled. This allows you to dictate the path used to access the controller.

Functions in controllers are exposed as routes by default. If you do not want to have a function exposed, prefix it with _.

Parameters

Parameters in functions are added in the order they are listed to expresses router. For example index(foo, bar, cb) will create the express route controller/:foo/:bar

You can also provide default parameters to functions. These parameters will act like normal get variables, but will default to the provided values if they are not provided.

class forum extends nmvc.base_controller {
 
//Will create the route forum/createPost/:subject/:body
//If you provide a ?note='foo' Than note will be set to 'foo', otherwise it will default to ''
    createPost(subject, body, note='', cb){
        //code
        cb(err, result);
    }
}

Every class must have a callback parameter named cb. The first parameter of cb is an error parameter, the second parameter is how the data should be handled. The second parameter is a json object.

    //passes the error back to the error handler
    cb(err)
    
    //Starts a download for the remote client
    cb(null, {
        download: {
            path:'path/to/file'
            name:'file_name'
        }
    })
    
    //Terminates the connection
    cb(null, {
        end: {
        
        }
    })
    
    //Sends a json object
    cb(null, {
        json: {
            foo:'bar'
        }
    })
    
    //Sends a jsonp object
    cb(null, {
        jsonp: {
            foo:'bar'
        }
    })
    
    //Redirects the user to the given path
    cb(null, {
        redirect: {
            code:300
            path:'/foo/bar'
        }
    })
    
    //Renders a template, passing it the provided data. Defaults to controller_name/function_name unless you pass a view param.
    cb(null, {
        render: {
            view:'/path/to/view/overide'
            data:{
                foo:'bar'
            }
        }
    })
    
    //Sends a status code to the remote client
    cb(null, {
        sendStatus: {
            code:300
        }
    })
    
    //Sends raw text to the remote client
    cb(null, {
        send: 'lorem ipsum...'
    })

Functions

There are 6 special functions in controllers that are treated differently than the rest:

'create', 'get', 'update', and 'delete' are treated as api functions and will append /api/ to the express route.

//This will create the route /api/foo/get/:id
class foo{
    get(id, cb){
        //code
        cb(null, {json:data});
    }
}

'index' is the default route for a controller and will have the route /controller/

'list' is a special case route that will append 's' to the end of a route function. You can overwrite this functionality by providing the class with a 'plural' variable.

//This will create the route /api/foo/get/:id
class goose{
    constructor(){
        //This will make it to the list function will create the path /geese instead of gooses
        this.plural='geese'
    }
    
    //By default this will create the route /gooses
    list(cb){
        //code
        cb(null, {render:data});
    }
}

Views

Views are jade based by default. See http://jade-lang.com/ for more information on syntax.

The default view directory is /views. every controller that uses a view should have a directory of it's own in the views directory.

A controller Foo with a function bar will render the view /views/foo/bar by default.

Models

Routes

Routes are auto-generated based on controller functions

Readme

Keywords

none

Package Sidebar

Install

npm i nmvc

Weekly Downloads

3

Version

0.0.15

License

none

Last publish

Collaborators

  • khadaj