25

I'm starting to get my head around node.js, and I'm trying to figure out how I would do normal MVC stuff. For example, here's a Django view that pulls two sets of records from the database, and sends them to be rendered in a template.

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

What might a similar node.js function look like?

Brian Tol
  • 4,149
  • 6
  • 24
  • 27
  • I published a blog post on the tools I used to get a solid MVC pattern going in Node here: http://www.travisglines.com/web-coding/a-simple-mvc-setup-in-node-js – Travis Feb 21 '11 at 02:32
  • I've found http://howtonode.org/ to be a big help in getting me up-to-speed. – Brian Tol Feb 19 '10 at 15:54

4 Answers4

13

http://boldr.net/mvc-stack-node-js-ejsgi-scylla-mustache is a great little article with a full github example of a MVC pattern using dirfferent Node modules. It also lists alternate modules currently available. It answered this question for me better than http://howtonode.org/ which has some good tuts but I couldn't find anything on MVC there.

Denis Hoctor
  • 2,597
  • 4
  • 33
  • 51
  • 1
    Github repository given on the blog (boldr.net I mean) is dead. If you happen to have a more up-to-date answer for this question can you share it with us, please? – scaryguy Dec 18 '14 at 21:22
0

TowerJS is a popular MVC framework based on

  • MongoDB (database)
  • Redis (background jobs)
  • CoffeeScript
  • Stylus
  • Jasmine (tests)
  • jQuery

Site http://towerjs.org/

Source https://github.com/viatropos/tower

250R
  • 35,945
  • 7
  • 33
  • 25
0

The easiest way to do this is with expressjs, which is an MVC framework for Node. Node is just what it says, evented I/O for the web.

The example on the http://expressjs.com should help with the basics but to answer your question directly.

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
Timothy Meade
  • 2,476
  • 1
  • 13
  • 9
  • 6
    It's possible to use Express together with other things to create an MVC framework, but Express is **not** "an MVC framework for Node". – callum Jan 04 '12 at 17:18
-1

RailwayJS is a MVC framework, written in JavaScript based on ExpressJS and runs over nodeJS platform. It is inspired by Ruby on Rails framework. You can read about MVC architecture of RailwayJS here: http://jsmantras.com/blog/RailwayJS-Routing

JS Mantras
  • 52
  • 5