Domanda

sto iniziando a ottenere la mia testa intorno node.js, e sto cercando di capire come avrei fatto normale roba MVC. Ad esempio, ecco una vista Django che tira due serie di record dal database, e invia loro di essere resi in un modello.

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

Quali potrebbero essere un simile node.js funzionare assomigliare?

È stato utile?

Soluzione

http://boldr.net/mvc-stack-node- js-ejsgi-scylla-baffi è un piccolo articolo con un esempio completo github di un pattern MVC utilizzando dirfferent Node moduli. Inoltre elenca moduli alternativi attualmente disponibili. E 'risposto a questa domanda per me meglio di http://howtonode.org/ che ha alcune tuts buoni ma non ho potuto trovare qualcosa sul MVC là.

Altri suggerimenti

Il modo più semplice per farlo è con expressjs, che è un framework MVC per Node. Il nodo è proprio quello che dice, evented I / O per il web.

L'esempio a http://expressjs.com dovrebbe aiutare con le nozioni di base, ma per rispondere direttamente alla tua domanda.

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 è un framework MVC popolare sulla base di

  • MongoDB (database)
  • Redis (processi in background)
  • CoffeeScript
  • Stylus
  • Jasmine (test)
  • jQuery

http://towerjs.org/

https://github.com/viatropos/tower

RailwayJS è un framework MVC, scritto in JavaScript sulla base di ExpressJS e corre oltre la piattaforma nodeJS. Si ispira framework Ruby on Rails. Si può leggere su MVC architettura del RailwayJS qui: http://jsmantras.com/blog/RailwayJS-Routing

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top