使用node.js进行丰富的应用开发 [英] Rich app development with node.js

查看:91
本文介绍了使用node.js进行丰富的应用开发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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


  1. 想要为所有功能开发RESTfull API。 (OAuth上的任何库?)

  2. 想要在API之上开发Web应用程序。
    应用程序必须以这样的方式设计,即在客户端开发主要功能。意味着,所有业务逻辑都必须在客户端开发。我听说像Backbone.js这样的库,Underscore.js已经完成了这项工作,但对它没有明确的想法。

请建议我哪些框架能更好地满足我的要求。



谢谢,

解决方案

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



服务器端:




  • Express.js

  • 把手

  • Passport.js

  • 猫鼬

  • MongoDB

  • Caolan表格(但我目前正在实施自己的表单处理程序)

  • Coffeescript



客户端:




  • 把手

  • Jquery

  • Require.js

  • Backbone.js

  • text.js(require.js的插件)

  • Coffeescript(require.js的插件。我的.coffee是使用r.js在dev和服务器端编译的客户端) / li>


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





<这是一个示例应用程序。



项目结构:

 表格
| ___ sampleForm.coffee
型号
| ___ sampleModel.coffee
public
| ___ images
| ___样式表
| | ___ style.less
| ___ sampleapp
| ___ main.js
| ___ cs.js
| ___ text.js
| ___ require.js
| ___集合
| | ___ 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

 要求( '咖啡脚本'); 
module.exports = require('./ application.coffee');

application.coffee

  ...标准express.js初始化
require(./ routes)(app)
... start server

index.coffee

  SampleModel = require../models/sampleModel
module.exports =(app)=>
app.get/ index,(req,res)=>
返回res.renderindex

app.get/ samplemodels,(req,res)=>
SampleModel.find {},(错误,型号)=>
返回res.send 404如果错误或!模型
返回res.send模型
返回

index.hbs

 <!DOCTYPE HTML> 
< html>
< head>
< title>示例应用< / title>
< link type =text / csshref =/ stylesheets / style.css =stylesheet>
< script src =/ mainapp / require.jsdata-main =/ mainapp / main>< / script>
< / head>
< body>
< div id =main-content>< / div>
< / body>
< / html>

main.js

  require.config({...})//配置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)=> ; 
类MainView扩展Backbone.View
initialize:=>
@ collection.onchange,@ render
@template = Hbs.compile模板
render:=>
html = @template {models:@ collection.models}
@ $ el.html(html)
return @

sampleTemplate.hbs

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

好的,这是必不可少的。现在你必须学习如何使用 Backbone.Collection Backbone.Model ,如何配置 Require.js ,如何配置 Passport.js 以及如何制作 Mongoose模型。您可以使用 Less中间件来编译您的style.less



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



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


I'm new to node.js, have given a requirement to develop a rich web based application using Node.js.

Right now I'm working on the getting started guides on node.js. I had a chance to look the page here and got confused with hundreds of frameworks. I have no idea to choose a suitable framework, and need help on this to make a perfect decision. Let me explain my requirement.

  1. Want to develop RESTfull API for all the functionalities. (Any libraries on OAuth?)
  2. Want to develop a web application on top of the API. The application has to be designed in such a way that major functionalities should be developed in the client side. Means, all the business logic has to be developed in the Client side. I heard some libraries like Backbone.js, Underscore.js already doing the job, but didn't have clear idea on it.

Please suggest me the frameworks which will do better for my requirement.

Thanks,

解决方案

Here is a good tech stack that I use for my applications:

Server side:

  • Express.js
  • Handlebars
  • Passport.js
  • Mongoose
  • MongoDB
  • Caolan forms (But I am currently in the process of implementing my own form handler)
  • Coffeescript

Client side:

  • Handlebars
  • Jquery
  • Require.js
  • Backbone.js
  • text.js (plugin for require.js)
  • Coffeescript (plugin for require.js. My .coffee are compiled client side in dev and server side in prod using r.js)

I might make a little sample app later if you want.

[EDIT]

ok here is a sample app.

Project structure:

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

Server side:

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}}

Ok so that is the essential. Now you'll have to learn how to use Backbone.Collection, Backbone.Model, how to configure Require.js, how to configure Passport.js and how to make a Mongoose model. You can use the Less middleware to compile your style.less

Don't forget that you can precompile all your client application with r.js.

Now I hope that this page will not be forgotten and that it will help anyone who come across it in the future.

这篇关于使用node.js进行丰富的应用开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆