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 = ;nmvc = ;nmvc;
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.nmvcenv = 'production'
By default NMVC runs with the following directory layout
- Root
- config
- dev.json
- helpers
- controllers
- models
- views
- assets
- public
- config
You can modify the directories used like so:
nmvc = ; nmvcconfig_dir = 'relative/directory/';nmvchelper_dir = 'relative/directory/';nmvcmodel_dir = 'relative/directory/';nmvccontroller_dir = 'relative/directory/';nmvcviews_dir = 'relative/directory/';nmvcpublic_dir = 'relative/directory/' nmvc;
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:
nmvcapp
Controllers
All controllers extend the base nmvc base controller. You can create your own base controller and extend that as well.
"use strict";moduleexports = base_controller { super; thisprefix = '/admin'; thisenabled = true; } { } //This function will not create a route. { };
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.
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 '' { //code ; }
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 //Starts a download for the remote client //Terminates the connection //Sends a json object //Sends a jsonp object //Redirects the user to the given path //Renders a template, passing it the provided data. Defaults to controller_name/function_name unless you pass a view param. //Sends a status code to the remote client //Sends raw text to the remote client
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 { //code ; }
'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 { //This will make it to the list function will create the path /geese instead of gooses thisplural='geese' } //By default this will create the route /gooses { //code ; }
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