Node.js MVC Constructionnode server.js visit Begin from Model Database 数据库定义 Operation 数据操作 数据定义 ORM BaseModel sequelize mongoose ...2014.jsconf.cn/slides/Rabbit.js-MVC.pdf ·

Embed Size (px)

Citation preview

  • Node.js MVC ConstructionRabbit.jsMVC

    @

  • Rabbit.jsa fast and light mvc framework for Nodejs

    https://github.com/xinyu198736/Rabbit.jssudo npm install rabbitjs -g

    https://github.com/xinyu198736/Rabbit.js

  • FAST LIGHT

  • why use it

    light

    fast development ,

    Multi-Layer Architecture

    Database agnostic unified API for sql and nosqlbase on ORM

  • View

    Multi-Layer

    Controller ModelService

    Filters

    request

  • Hello Worldsudo npm install rabbitjs -g

    rabbit create

    npm install

    node server.js

    visit http://localhost:8080

    http://localhost:8080

  • Begin from Model

    Database Operation

    ORM BaseModel

    sequelize mongoose

  • module.exports = { id: { type: "int", autoIncrement: true, primaryKey: true }, name: "varchar(255)", desc: "text", age: "int" };

    Mysql(teacher.js) MongoDB(teacher.js)

    module.exports = { name: String, desc: String, age: Number }

    var Teacher = new BaseModel(teachers);

    var Teacher = new BaseModel(teachers,mongo);

  • Promise var Teacher = new BaseModel('teachers');//Teacher.getAll().where({id:{lt:3}}).offset(0).limit(10).order({id:desc}).fields([id','name','age']).done(function(error,teachers){!})

    ORMTeacher.Model

    getAll!getById !getByField !update!delete!count !add!addCount !

    where !offset!limit !fields !order

  • Then Serviceservice

    Keep controller clear

    controller

  • Controllers

    Base on Express.js

  • Restfull

    Auto generate routes by file structureURL: http://localhost:8080/teacher/add

    File System: controllers/teacher/index.js

    module.exports = {

    /add:{

    get:function(){

    return function(req,res,next){

    next();

    }

    }

    }}

    Auto Render Views

    rabbit controller -n teacher #controller

    http://localhost:8080/teacher/add

  • Filters

    preFilter

    afterFilterjsonp

    globalFilterfilter

    middleware

  • var service_teacher = loadService(teacher);module.exports = {

    /add:{

    get:function(){

    this.userFilter([checkLogin]);

    this.userAfterFilter([jsonp]);

    return function(req,res,next){

    service_teacher.add(req.query)

    .done(function(error,teacher){

    if(error) next(error);

    else next();

    });

    }

    }

    }}

    module.exports = { /example/.*': { get: ['checkLogin'] }}

    filterfilter.config.js

  • Views

    Jade

    res.render(teacher/add.jade) or auto render

    res.locals

    app.locals.moment = require(moment);

  • OthersTiming tasks

    ,crontab

    plugin

    scaffoldmodel

  • Other MVC frameworksSails.js

    Geddy.js

    Express.js

    like ruby on rails ,realtime framework

    A simple, structured web framework for Node

    much more base librarynetwork framework

  • Thanks

  • @

    F2Emier963@