【问题标题】:Rich app development with node.js使用 node.js 开发丰富的应用程序
【发布时间】:2013-02-04 04:39:38
【问题描述】:

我是 node.js 的新手,提出了使用 Node.js 开发基于丰富 Web 的应用程序的要求。

现在我正在编写关于 node.js 的入门指南。 我有机会查看here 页面并与数百个框架混淆。我不知道选择一个合适的框架,需要帮助才能做出完美的决定。让我解释一下我的要求。

  1. 想要为所有功能开发 RESTfull API。 (OAuth 上有任何库吗?)
  2. 希望在 API 之上开发 Web 应用程序。 应用程序的设计方式必须使主要功能应在客户端开发。意味着,所有的业务逻辑都必须在客户端开发。我听说 Backbone.js、Underscore.js 等一些库已经在做这项工作,但并没有明确的想法。

请向我推荐可以更好地满足我的要求的框架。

谢谢,

【问题讨论】:

  • 没有一百个 MVC 框架,最多 3/4 服务器端和相同数量的客户端。
  • @mpm:我认为你低估了。 TodoMVC 列出了超过 25 个客户端框架,并且不仅仅是编译成客户端 JS,以及许多其他类别。
  • @Scott Sauyet 这些框架中有多少在互联网上有扩展文档、博客文章、支持和教程?这就是你如何判断一个框架是否值得使用。从采用的角度来看,这些框架中有 80% 是无关紧要的。
  • @mpm:哪些无关紧要?除了三个之外,我都听说过,并尝试了大约一半。在您愿意考虑之前,框架必须被广泛采用吗?

标签: javascript node.js


【解决方案1】:

这是我用于我的应用程序的一个很好的技术堆栈:

服务器端:

  • Express.js
  • 车把
  • Passport.js
  • 猫鼬
  • MongoDB
  • 草栏表单(但我目前正在实现自己的表单处理程序)
  • 咖啡脚本

客户端:

  • 车把
  • jQuery
  • Require.js
  • Backbone.js
  • text.js(require.js 插件)
  • Coffeescript(require.js 的插件。我的 .coffee 是在 dev 中编译客户端,在 prod 中使用 r.js 编译服务器端)

如果你愿意,我稍后可能会制作一个小示例应用程序。

[编辑]

好的,这是一个示例应用程序。

项目结构:

forms
  |___ sampleForm.coffee
models
  |___ sampleModel.coffee
public
  |___ images
  |___ stylesheets
  | |___ style.less
  |___ sampleapp
    |___ main.js
    |___ cs.js
    |___ text.js
    |___ require.js
    |___ collections
    | |___ sampleCollection.coffee
    |___ models
    | |___ sampleModel.coffee
    |___ templates
    | |___ sampleTemplate.hbs
    |___ lib
    | |___ handlesbars.js
    | |___ backbone.js
    | 
    | |___ ...
    |___ views
      |___ sampleView.coffee
routes
  |___ index.coffee
views
  |___ index.hbs
app.js
application.coffee
package.json

服务器端:

app.js

require('coffee-script');
module.exports = require('./application.coffee');

application.coffee

... standard express.js initialization
require("./routes")(app)
... start server

index.coffee

SampleModel = require "../models/sampleModel"
module.exports = (app) =>
  app.get "/index", (req,res) =>
    return res.render "index"

  app.get "/samplemodels", (req,res) =>
    SampleModel.find {}, (err, models) =>
      return res.send 404 if err or !models
      return res.send models
    return

index.hbs

<!DOCTYPE HTML>
<html>
<head>
  <title>Sample app</title>
  <link type="text/css" href="/stylesheets/style.css" rel="stylesheet" >
  <script src="/mainapp/require.js" data-main="/mainapp/main"></script>
</head>
<body>
  <div id="main-content"></div>
</body>
</html>

ma​​in.js

require.config({...}) // Configure requires.js...

require(["jquery", "cs!models/samplemodel", "cs!views/sampleview","cs!collections/samplecollection"], function ($, Model, View, Collection) {
  var collection = new Collection();
  collection.fetch();
  var view = new View({collection: collection});
  $("body").html(view.render().$el);
})

sampleview.coffee

define ["backbone", "jquery", "handlebars","text!templates/sampleTemplate.hbs"], (Backbone, $, Hbs, template) =>
  class MainView extends Backbone.View
    initialize: =>
      @collection.on "change", @render
      @template = Hbs.compile template
    render: =>
      html = @template {models: @collection.models}
      @$el.html(html)
      return @

sampleTemplate.hbs

{{#models}}
  <p>{{name}}</p>
{{/models}}

好的,这是必不可少的。现在你必须学习如何使用Backbone.CollectionBackbone.Model,如何配置Require.js,如何配置Passport.js以及如何制作Mongoose model。你可以使用Less middleware来编译你的style.less

不要忘记您可以使用 r.js 预编译所有客户端应用程序。

现在我希望这个页面不会被遗忘,它会帮助将来遇到它的任何人。

【讨论】:

  • 谢谢,你能给我做一个小示例应用吗
  • 我会用 Jade 替换这个列表中的 Handlebars,但其余的都很好。
  • Jade 更灵活,但在我看来,它与真正的 html 相去甚远。另外,我认为 Jade 导致一些开发人员在视图层中编写了很多逻辑。
【解决方案2】:

这是一篇很棒的文章,有助于解释最流行的 javascript 框架:

http://coding.smashingmagazine.com/2012/07/27/journey-through-the-javascript-mvc-jungle/

最终,最好的方法是列出您认为对您有帮助的框架的简短列表,然后对每个框架进行一些尝试,看看哪个最适合您的应用程序和编程风格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 2010-11-18
    • 1970-01-01
    • 2011-12-02
    • 2014-11-24
    • 1970-01-01
    • 2014-11-02
    相关资源
    最近更新 更多