simple and lightweight framework to improve Express JS MVC's architecture
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).
npm install express-simple-mvc
Create your express application as usual and use the "simple_mvc" function
express = require 'express'require 'express-simple-mvc'require 'namespace'app = expressapplisten 3000simple_mvc app
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 /players/:id controllers.Players.show GET /players/:id/edit controllers.Players.edit POST /players controllers.Players.create PUT /players/:id controllers.Players.update DELETE /players/: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 namespacemodels# GET /players: =>Playerall@ressend players# GET /players/:id: =>Playerread @reqparamsid@ressend player# GET /players/new: =>@resrender 'newForm'# GET /players/:id/edit: =>Playerread @reqparamsid@resrender 'editForm'player: player# POST /players: =>Playercreate @reqparam'player'=>@ressend 'New player successfully created !'# PUT /players/:id: =>Playerupdate @reqparamsid@reqparam'player'=>@ressend "Player successfully updated !"# DELETE /players/:id: =>Playerdelete @reqparamsid->@ressend "Player successfully deleted !"
Properties @req and @res are automatically send to the controller, you don't need to pass them as params as you would do before with Express
And the for the model (I use mongoose here, but you can adapt it)
mongoose = require 'mongoose'namespace models:@schema =pseudo: Stringx:type: Numberdefault: 0y:type: Numberdefault: 0@model = mongoosemodel 'Player' @schema:@modelfind nullif err then throw errcb docs:new @modeldocsaveif err then throw errdo cb:@modelfindById _idif err then throw errcb doc:@modelfindByIdAndUpdate _iddocif err then throw errdo cb:@modelfindByIdAndRemove _idif err then throw errdo cb
And that's all ! Looks difficult ? Have a look at the source code in /src (only 50 lines !) or run the example.