使用node.js进行丰富的应用开发

时间:2013-02-19 13:17:39

标签: javascript node.js

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

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

  1. 想要为所有功能开发RESTfull API。 (OAuth上的任何库?)
  2. 想要在API之上开发Web应用程序。 应用程序必须以这样的方式设计,即应在客户端开发主要功能。意味着,所有业务逻辑都必须在客户端开发。我听说像Backbone.js这样的库,Underscore.js已经完成了这项工作,但对它没有明确的想法。
  3. 请建议我的框架能够更好地满足我的要求。

    谢谢,

2 个答案:

答案 0 :(得分:15)

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

服务器端:

  • Express.js
  • 车把
  • Passport.js
  • 猫鼬
  • MongoDB的
  • Caolan表单(但我目前正在实现自己的表单处理程序)
  • 的CoffeeScript

客户方:

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

如果你愿意,我可以稍后制作一个小样本应用程序。

[编辑]

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

项目结构:

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>

<强> main.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预编译所有客户端应用程序。

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

答案 1 :(得分:2)

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

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

最终,最好的方法是制作一个你认为可以帮助你的框架的简短列表,然后只需稍微弄清楚每个框架,看看哪个最适合你的应用程序和编程风格。

相关问题