类似MVC的代码如何在Node.js中工作?

我开始在node.js的头上,我想弄清楚如何做正常的MVC的东西。 例如,下面是一个Django视图,它从数据库中提取两组logging,并将它们发送到模板中。

def view(request): things1 = ThingsOne.objects.all() things2 = ThingsTwo.objects.all() render_to_response('template.html, {'things1': things1, 'things2': things2}) 

什么可能类似的node.js函数看起来像?

最简单的方法是使用expressjs,它是Node的MVC框架。 节点正是它所说的,即使是networking的I / O。

http://expressjs.com上的例子应该帮助基础知识,但直接回答你的问题。

 var express = require('express'); var app = express.createServer(); app.get('/whatever', function(req, res) { Things1.objects.getAll(function(things1) { Things2.objects.getAll(function(things2) { var options = { locals: { things1: things1, things2: things2 }}; res.render('thingstemplate.ejs', options); // or thingstemplate.jade or whatever }); }); }); app.listen('80', ''); // port and optional hostname to bind 

TowerJS是一个stream行的基于MVC的框架

  • MongoDB(数据库)
  • Redis(后台作业)
  • CoffeeScript的
  • 唱针
  • 茉莉花(testing)
  • jQuery的

网站http://towerjs.org/

来源https://github.com/viatropos/tower

RailwayJS是一个基于ExpressJS的JavaScript编写的MVC框架,运行在nodeJS平台上。 它受Ruby on Rails框架的启发。 您可以在这里阅读有关RailwayJS的MVC体系结构: http ://jsmantras.com/blog/RailwayJS-Routing