# express-simple-mvc

> simple and lightweight framework to improve Express JS MVC's architecture

Latest version **0.2.0** (published 2013-02-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-simple-mvc
pnpm add express-simple-mvc
yarn add express-simple-mvc
bun add express-simple-mvc
```

## Health

**Score 5/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: insecure dependencies; abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2013-02-22 |
| First published | 2013-02-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | MaksJS |
| Maintainers | maksjs |

## Links

- npm: https://www.npmjs.com/package/express-simple-mvc
- Repository: https://github.com/MaksJS/Express-Simple-MVC
- npm.io page: https://npm.io/package/express-simple-mvc

## Dependencies (1)

- [namespace](https://npm.io/package/namespace.md) git://github.com/MaksJS/Namespace-in-CoffeeScript.git

## Recent versions

- 0.2.0 (latest) — 2013-02-22
- 0.1.1 — 2013-02-20
- 0.1.0 — 2013-02-19

## README

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.

```coffeescript
express      = require 'express'
{simple_mvc} = require 'express-simple-mvc'
require 'namespace'

app = express()
app.listen 3000

app.configure ->
  app.use express.methodOverride()
  app.use app.router

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)

```coffeescript
namespace controllers:

  class Players

    # Import Player model from models namespace
    {Player} = models

    # GET /players
    @index: (req, res) ->
      Player.all (players) ->
        res.send players

    # GET /player/:id
    @show: (req, res) ->
      Player.read req.params.id, (player) ->
        res.send player

    # GET /players/new
    @new: (req, res) ->
      res.render 'newForm'

    # GET /player/:id/edit
    @edit: (req, res) ->
      Player.read req.params.id, (player) ->
        res.render 'editForm'
          player: player

    # POST /players
    @create: (req, res) ->
      Player.create req.param('player'), ->
        res.send 'New player successfully created !'

    # PUT /player/:id
    @update: (req, res) ->
      Player.update req.params.id, req.param('player'), ->
        res.send "Player #{req.param('id')} successfully updated !"

    # DELETE /player/:id
    @delete: (req, res) ->
      Player.delete req.params.id, ->
        res.send "Player #{req.param('id')} successfully deleted !"
```

And the for the model (I use mongoose here, but you can adapt it)

```coffeescript
mongoose = require 'mongoose'

namespace models:

  class Player

    @schema =
      pseudo: String
      x:
        type: Number
        default: 0
      y:
        type: Number
        default: 0

    @model = mongoose.model 'Player', new mongoose.Schema @schema

    @all: (cb) ->
      @model.find null, (err, docs) ->
        if err then throw err
        cb docs

    @create: (doc, cb) ->
      new @model(doc).save (err) ->
        if err then throw err
        do cb

    @read: (_id, cb) ->
      @model.findById _id, (err, doc) ->
        if err then throw err
        cb doc

    @update: (_id, doc, cb) ->
      @model.findByIdAndUpdate _id, doc, (err) ->
        if err then throw err
        do cb

    @delete: (_id, cb) ->
      @model.findByIdAndRemove _id, (err) ->
        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.

---
_Source: https://npm.io/package/express-simple-mvc · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
