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

发布于 2024-08-20 14:14:04 字数 321 浏览 7 评论 0原文

我开始了解 Node.js,并试图弄清楚如何执行正常的 MVC 操作。例如,下面是一个 Django 视图,它从数据库中提取两组记录,并将它们发送到模板中进行渲染。

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

类似的 Node.js 函数会是什么样子?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

胡渣熟男 2024-08-27 14:14:04

http://boldr.net/mvc-stack-node-js-ejsgi -scylla-mustache 是一篇很棒的小文章,其中包含使用不同 Node 模块的 MVC 模式的完整 github 示例。它还列出了当前可用的替代模块。它比 http://howtonode.org/ 更好地回答了我这个问题,后者有一些很好的建议,但我不能在那里找到有关 MVC 的任何内容。

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.

硬不硬你别怂 2024-08-27 14:14:04

最简单的方法是使用expressjs,它是 Node.js 的 MVC 框架。 Node 顾名思义,就是用于网络的事件化 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

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
梦在夏天 2024-08-27 14:14:04

TowerJS 是一个流行的 MVC 框架,基于

  • MongoDB(数据库)
  • Redis(后台作业)
  • CoffeeScript
  • Stylus
  • Jasmine(测试)
  • jQuery

站点 http://towerjs .org/

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

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

感情废物 2024-08-27 14:14:04

RailwayJS是一个MVC框架,基于ExpressJS用JavaScript编写,运行在nodeJS平台上。它的灵感来自于 Ruby on Rails 框架。您可以在这里了解RailwayJS的MVC架构:http://jsmantras.com/blog/RailwayJS-Routing

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文