Express Simple MVC
Very simple and lightweight framework to improve Express JS MVC's architecture written in CoffeeScript. It provides a simple way to create models and controllers with an object-oriented approach (CoffeeScript).
How to install
npm install express-simple-mvc
How it works
Create your express application as usual and use the "simple_mvc" function and pass the path to the controllers, models and routes directories.
express = require 'express' require 'express-simple-mvc'require 'namespace' app = expressapplisten 3000 appconfigure -> appuse expressmethodOverride appuse approuter simple_mvc app__dirname + '/controllers'__dirname + '/models'__dirname + '/config/routes'
You must configure your application before using simple_mvc. You'll also need a namespace pattern for coffeescript, you can install it with npm install
Create a directory called "config" with a file called "routes" inside. Define your routes (inspired by Play! Framework), for example :
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
GET / controllers.Players.index
GET /players controllers.Players.index
GET /players/new controllers.Players.new
GET /player/:id controllers.Players.show
GET /player/:id/edit controllers.Players.edit
POST /players controllers.Players.create
PUT /player/:id controllers.Players.update
DELETE /player/:id controllers.Players.delete
Create a models and a controllers directory with a Players controller and a Player model. For example, simple CRUD (don't forget the namespace)
namespace controllers: # Import Player model from models namespace models # GET /players : Playerall ressend players # GET /player/:id : Playerread reqparamsid ressend player # GET /players/new : resrender 'newForm' # GET /player/:id/edit : Playerread reqparamsid resrender 'editForm' player: player # POST /players : Playercreate reqparam'player'-> ressend 'New player successfully created !' # PUT /player/:id : Playerupdate reqparamsidreqparam'player'-> ressend "Player successfully updated !" # DELETE /player/:id : Playerdelete reqparamsid-> ressend "Player successfully deleted !"
And the for the model (I use mongoose here, but you can adapt it)
mongoose = require 'mongoose' namespace models: @schema = pseudo: String x: type: Number default: 0 y: type: Number default: 0 @model = mongoosemodel 'Player' @schema : @modelfind null if err then throw err cb docs : new @modeldocsave if err then throw err do cb : @modelfindById _id if err then throw err cb doc : @modelfindByIdAndUpdate _iddoc if err then throw err do cb : @modelfindByIdAndRemove _id if err then throw err do cb
And that's all ! Looks difficult ? Have a look at the source code in /src (only about 20 lines !) or run the example.