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 and pass the path to the controllers, models and routes directories.
express = require 'express'require 'express-simple-mvc'require 'namespace'app = expressapplisten 3000appconfigure ->appuse expressmethodOverrideappuse approutersimple_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 namespacemodels# GET /players:Playerallressend players# GET /player/:id:Playerread reqparamsidressend player# GET /players/new:resrender 'newForm'# GET /player/:id/edit:Playerread reqparamsidresrender '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: 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 about 20 lines !) or run the example.