草庐IT

node.js - RailJS 与 TowerJS

coder 2023-05-28 原文

就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the help center为指导。




8年前关闭。




再次...选择框架。我已经停在这两个 TowerJS 和 RailwayJS 上,但它们看起来非常相似,很难选择哪种方式

两者都基于 Express,两者都是 RoR 风格的框架......

哪一款最有前途,哪一款会更受欢迎?

或者也许我已经走错路了?也许我应该选择其他框架。

我讨厌有这么多框架可供选择,没有可以依赖的行业标准,或多或少地确定该框架将在近两年内开发出来......

请帮助,需要专家的建议。谢谢

最佳答案

这是一个简要的概述表,我将在下面讨论一些内容。

+-----------------------+------------------------------+------------------------------------+
|                       | RailwayJS                    | Tower.js                           |
+-----------------------+------------------------------+------------------------------------+
| First commit          | Jan 2011                     | Oct 2011                           |
| Rails                 | 2.3.x                        | 3.x                                |
| Node.js               | >= 0.4.x                     | >= 0.4.x                           |
| Server                | ✓                            | ✓                                  |
| Client                |                              | ✓                                  |
| Template agnostic     | ✓                            | ✓                                  |
| Default engine        | EJS                          | CoffeeKup                          |
| Database agnostic     | ✓                            | ✓                                  |
| Default datastore     | MongoDB                      | MongoDB                            |
| Model validations     | validatesPresenceOf('email') | validates('email', presence: true) |
| Query scopes          | ✓                            | ✓                                  |
| Chainable scopes      |                              | ✓                                  |
| Param parsing         |                              | ✓                                  |
| Controllers           | ✓                            | ✓                                  |
| Resource controllers  |                              | ✓                                  |
| File naming           | users_controller.js          | usersController.coffee             |
| vm.runInCustomContext | ✓                            |                                    |
| Asset pipeline        |                              | ✓                                  |
| Asset compression     |                              | ✓                                  |
| Routing               | map.resources('posts')       | @resources 'posts'                 |
| Nested routes         | ✓                            | ✓                                  |
| Generated url helpers | ✓                            |                                    |
| Generators            | ✓                            | ✓                                  |
| Command-line api      | ✓                            | ✓                                  |
| REPL (console)        | ✓                            | ✓                                  |
| CoffeeScript console  |                              | ✓                                  |
| Asset cache method    | timestamp                    | md5 hash                           |
| Production asset path | /app.css?123123123           | /app-859c828c89288hc8918741.css    |
| Preferred Language    | JavaScript                   | CoffeeScript                       |
| CoffeeScript support  | ✓                            | ✓                                  |
| Internationalization  | ✓                            | ✓                                  |
| Heroku support        | ✓                            | ✓                                  |
| String case           | snake_case                   | camelCase                          |
| Form builder          | ✓                            | ✓                                  |
| Semantic form builder |                              | ✓                                  |
| Table builer          |                              | ✓                                  |
| File watcher API      |                              | ✓                                  |
| Live-reload assets    |                              | ✓                                  |
| Test suite            |                              | ✓                                  |
| Generators for tests  |                              | ✓                                  |
| Twitter Bootstrap     | ✓                            | ✓                                  |
| HTML5 Boilerplate     |                              | ✓                                  |
+-----------------------+------------------------------+------------------------------------+

I created Tower.js to achieve several goals which none of the existing frameworks did adequately. Here are some of those goals.

1. Same code on the client and server

Since Node.js made JavaScript possible on the server, there's no reason to be writing one part of the app in Rails, and the other in Backbone. That's anything but DRY. You should be able to define the models once and use them on both the client and the server.

RailwayJS only works on the server because it was built around express. Tower.js is also built around express but in a way that makes it work for both the client and server. Tower.js provides the same exact API for the client and server. This meant I had to rewrite some things like the router so it works the same on the client and the server (plus it allows you to do things like history.pushState with the # fallback, using the same set of routes).

2. Same "views" on the client and server

I spent a lot of time in Rails and writing Haml templates. Alongside I was writing web and mobile JavaScript interfaces using template languages like Mustache. That's more code duplication… You should be able to use the same set of views/templates on both the client (as JavaScript templates) and server (rendering static HTML).

Since Haml was pretty awesome (super clean, allowed you to execute arbitrary ruby, built in pretty-printing, etc.), the closest JavaScript alternative was CoffeeKup. And it works on both the client and server. CoffeeKup allows you to write templates with all the power of JavaScript, so you have no limitations. Building a FormBuilder in Mustache is either going to take a lot of work or a lot of code, or both.

Do note though, you're free to swap out template engines and use Jade, Mustache, Handlebars, etc. for the client or server. CoffeeKup is just a clean and powerful default.

3. Rails-quality model API on the client and server

ActiveModel (implemented by ActiveRecord for SQL and Mongoid for MongoDB for Rails) is a very thorough and well-tested API allowing developers to define and interact with data. It's both powerful and enjoyable. All of the previous (and current) JavaScript implementations were never close to as robust and well designed, and I didn't see anything happening in the near future.

If you can write this in Rails:

User.where(:email => /[a-z/).page(2).limit(20)

你应该能够在 JavaScript 中做到这一点:
App.User.where(email: /[a-z/).page(2).limit(20)

Tower.js 带有“可链式作用域”,意思是核心查询 + 分页。它模仿了 MongoDB Query API ,但此 API“输入”将转换为适用于不同数据存储的数据库命令。

4. 统一的 SQL 和 NoSQL 数据存储接口(interface)

Tower.js 目前拥有 MongoDB 和 Memory(浏览器内)存储,旨在为其余流行数据库(CouchDB、Neo4j、PostGreSQL、MySQL、SQLite、Cassandra 等)提供统一接口(interface)。

RailJS 似乎也通过 JugglingDB 做到了这一点,这看起来是一个好的开始。但我选择不使用它有几个原因。首先,它看起来是围绕 Rails 2.x API 构建的( User.validatesUniquenessOf "email"User.validates "email", presence: true )。其次,它没有 Rails 3 那样丰富的可链接查询。第三,我希望能够快速地将代码添加到代码库中,而且由于我非常挑剔,我可能最终会重构整个事情以使用 CoffeeScript,哈哈。而且我不想围绕它构建一个层,因为它也必须在客户端上工作,因此保持库架构尽可能小是一个高优先级。

5.资源丰富的 Controller

inherited_resources Ruby gem 从我的 Rails Controller 中删除了大约 90% 的代码。它找出了一组用于实现 7 个基本 Controller 操作的约定。 Tower.js 包含这样的内容,因此默认情况下您不必在 Controller 中编写任何代码,它们仍然会以 JSON 和 HTML 进行响应。它还使您可以定义嵌套路由。

6. 自动 URL 到数据库查询解析器

在 Tower.js 中,您可以告诉 Controller 监视 url 中的特定参数,并将它们转换为准备应用于模型查询的哈希。
class App.UsersController extends App.ApplicationController
  @param "email"

  index: ->
    App.User.where(@criteria()).all (error, users) =>
      @respondTo (format) =>
        format.json => @render json: users
        format.html => @render "index", locals: {users}

给定一个类似于 /users?email=abc&something=random 的网址,然后 @criteria()会给你一个哈希{email: /abc/} .

它不在 Rails 中,但我希望它是。

7. 语义形式

我非常喜欢语义 HTML。 Rails 的表单生成器生成的 HTML 非常难看,所以很多人和我都用过 Formtastic ,产生更多的语义形式。 Tower.js 使用与 Formtastic 几乎相同的 API。它还具有语义表构建器,这使得为管理 View 构建可搜索/可排序的表变得非常容易。

8. Assets 管道

Rails 3 有一个很棒的 Assets 管道,你可以用 CoffeeScript 编写 JavaScript,用 SCSS 编写 CSS,它会自动重新编译。然后rake assets:precompile您的 Assets ,您将获得为 S3 准备的 md5 哈希压缩 Assets 。自己构建起来非常困难,而且我没有看到有人为 Node.js 做这方面的工作。

RailwayJS 使用 Rails 2 方法为 Assets 路径加上时间戳,所以不是这个 md5 哈希版本:
/stylesheets/application-51e687ad72175b5629f3b1538b65ea2c.css

你会得到这样的东西:
/stylesheets/application.css?1306993455524

由于几个重要原因,这是一个问题。 Rails Asset Pipeline Guide有细节,但最重要的是 S3 不识别时间戳,所以它正在读取/stylesheets/application.css,如果你设置了一个很远的 future Expires标题并且您更改了 CSS,之前访问过您网站的任何人都必须清除其缓存或强制刷新您的页面才能看到更新。

RailJS 也没有内置的 Assets 编译管道(至少据我所知)。

9.监视文件

Guard在 Rails 中是一个巨大的生产力助推器。它允许您编写快速的“监视任务”,本质上类似于 rake/cake 任务,这些任务在创建/更新/删除与模式匹配的文件时运行。

塔有这个内置(使用 design.io )。这实际上是告诉 CoffeeScript 和 Stylus Assets 编译成 JavaScript 和 CSS 的原因。但是您可以使用此功能做非常强大的事情,请参阅 https://github.com/guard/guard/wiki/List-of-available-Guards举些例子。

10. CoffeeScript

CoffeeScript 的忠实粉丝。

CoffeeScript 将您需要编写的 JavaScript 数量减少了一半(6,501 additions, 15,896 deletions 将整个 Node.js 库转换为 CoffeeScript)。它使编码变得更快、更容易。

此外,CoffeeScript 是保持 Rails 向世界展示的高效和愉快的编码体验的唯一方法。 JavaScript 不会那样做。

小事

我是标准的粉丝。 RailJS 坚持使用 snake_case 的 Ruby 约定,我也想这样做,但 JavaScript 社区使用 camelCase,所以 Tower 也采用了它。 CamelCase 也有一些额外的好处,例如您不需要为客户端将服务器端 Rails snake_case 转换为/从 camelCase 转换,并且删除额外的字符可以让您的文件更小。

我也喜欢 super 干净的代码。在我考虑为一个项目做贡献之前,我通读了源代码......如果它非常困惑,我可能会重写它。

我也喜欢优化代码。对于 Tower.js,一个很大的目标是对其进行结构化,使其能够完成 Rails 所做的一切,在客户端和服务器中提供完全相同的 API,使用尽可能少的代码。但是,在最小化代码库的大小和编写清晰且有趣/高效的代码之间存在权衡。仍在寻找两全其美的方法。

我也绝对是长期参与的。这是我们公司的基础,也是我个人将在 future 建立的一切。我想达到这样的程度,您可以在一天内推出一款设计精美、功能强大且高度优化的应用程序。

希望有帮助。

关于node.js - RailJS 与 TowerJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9897017/

有关node.js - RailJS 与 TowerJS的更多相关文章

  1. ruby-on-rails - Assets 管道损坏 : Not compiling on the fly css and js files - 2

    我开始了一个新的Rails3.2.5项目,Assets管道不再工作了。CSS和Javascript文件不再编译。这是尝试生成Assets时日志的输出:StartedGET"/assets/application.css?body=1"for127.0.0.1at2012-06-1623:59:11-0700Servedasset/application.css-200OK(0ms)[2012-06-1623:59:11]ERRORNoMethodError:undefinedmethod`each'fornil:NilClass/Users/greg/.rbenv/versions/1

  2. ruby-on-rails - Rails - 理解 application.js 和 application.css - 2

    rails新手。只是想了解\assests目录中的这两个文件。例如,application.js文件有如下行://=requirejquery//=requirejquery_ujs//=require_tree.我理解require_tree。只是将所有JS文件添加到当前目录中。根据上下文,我可以看出requirejquery添加了jQuery库。但是它从哪里得到这些jQuery库呢?我没有在我的Assets文件夹中看到任何jquery.js文件——或者直接在我的整个应用程序中没有看到任何jquery.js文件?同样,我正在按照一些说明安装TwitterBootstrap(http:

  3. node.js - 如何在 Travis CI 上的一个项目中运行 Node.js 和 Ruby 测试 - 2

    我有一个包含多个组件的存储库,其中大部分是用JavaScript(Node.js)编写的,一个是用Ruby(RubyonRails)编写的。我想要一个.travis.yml文件来触发一个运行每个组件的所有测试的构建。根据thisTravisCIGoogleGroupthread,目前还没有官方支持。我的目录结构是这样的:.├──构建服务器├──核心├──扩展├──网络应用├──流浪文件├──package.json├──.travis.yml└──生成文件我希望能够运行特定版本的Ruby(2.2.2)和Node.js(0.12.2)。我已经有了一个make目标,所以maketest在每

  4. ruby-on-rails - ruby open ssl api for encrypted key (without nodes option) - 2

    在安装了openssllib的linux机器上,当您执行带有“-nodes”选项的“opensslpkcs12”时,您将获得带有未加密私钥的输出,但如果您跳过–nodes选项,则输出将具有加密的私钥。e.g.opensslpkcs12-intest.pfx-outtest.pem你应该看到像下面这样加密的私钥-----BEGINENCRYPTEDPRIVATEKEY-----MIIFDjBABgkqhkiGG7s=-----ENDENCRYPTEDPRIVATEKEY-----如何使用ruby​​的开放ssl库实现上述目标?这就是我用ruby​​生成私钥的方式:@private_key

  5. node.js - 从未编写过任何自动化测试,我应该如何开始行为驱动开发? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。多年来,我一直在使用多种语言进行编程,并且认为自己总体上相当擅长。但是,我从未编写过任何自动化测试:没有单元测试,没有TDD,没有BDD,什么都没有。我已经尝试开始为我的项目编写适当的测试套件。我可以看到在进行任何更改后能够自动测试项目中所有代码的理论值(value)。我可以看到像RSpec和Mocha这样的测试框架应该如何使设置和运行所述测试变得相当容易

  6. ruby-on-rails - 将 Angular JS 与 Rails 集成 - 2

    我需要一些指导来了解如何将Angular整合到rails中。选择Rails的原因:我喜欢他们偏执的做事方式。还有迁移,gem真的很酷。使用angular的原因:我正在研究和寻找最适合SPA的框架。Backbone似乎太抽象了。我不得不在Angular和Ember之间做出选择。我首先开始阅读Angular,它对我来说很有意义。所以我从来没有去读过关于ember的文章。使用Angular和Rails的原因:我研究并尝试使用小型框架,例如grape、slim(是的,我也使用php)。但我觉得需要坚持项目的长期范围。我个人喜欢用Rails的方式做事。这就是我需要帮助的地方,我在Rails4中有

  7. node.js - 如何比较图像并确定哪个内容更多? - 2

    目标:我想从动画GIF中抓取最佳帧并将其用作静态预览图像。我相信最好的帧是显示最多内容的帧-不一定是第一帧或最后一帧。以这张动图为例:--这是第一帧:--这是第28帧:很明显,第28帧很好地代表了整个GIF。我如何以编程方式确定一帧是否比另一帧具有更多像素/内容?如果您能向我指出任何想法、想法、包/模块或文章,我们将不胜感激。 最佳答案 实现此目的的一种直接方法是估计entropy每个图像的帧,并选择具有最大熵的帧。在信息论中,熵可以被认为是图像的“随机性”。单一颜色的图像是非常可预测的,分布越平坦,越随机。这与Arthur-R描述

  8. css - Less.js LoadError - 没有这样的文件要加载 - less 在 main .less 文件上 - 2

    我正在尝试在一个新元素中测试less.js。我100%确定javascript文件加载正常,但我的css文件夹中的less文件一直出现此错误。我打算在启动之前使用less.app编译它,但我宁愿让less.js在开发期间进行编译。由于它是.less或其他文件而不允许浏览器访问该文件是否存在问题?谢谢(运行ubuntu11.04和ruby​​v1.9.2,在firefox和chrome中同样的错误)这是我的head.haml文件,我在其中链接到less.js和app.less(它们都在正确的文件夹中)%link{:rel=>"stylesheet/less",:type=>"text/c

  9. ruby - 通过 node-sass 的 Symfony assetic sass 过滤器? - 2

    我在让asseticsass过滤器与node-sass而不是ruby​​替代品一起工作时遇到了一些困难。我的config.yml文件中有以下配置:assetic:debug:"%kernel.debug%"use_controller:falsebundles:[]write-to:"%kernel.root_dir%/../web/assets"read_from:"%kernel.root_dir%/../web/assets"node:"%%PROGRAMFILES%%\nodejs\\node.exe"node_paths:["%%USERPROFILE%%\\AppData\

  10. ruby - I18n::InvalidLocaleData:I18n gem 在 js 导出中有问题 - 2

    我正在OSX10.13.4上使用Rails3.2.22.4、Ruby2.2.7开发一个应用程序。有多个翻译文件,例如。en.yml,sq.yml基于国家。从下面的ruby​​mine执行命令时,加载网页时出现错误。rake--tracei18n:js:export来自en.yml的yaml内容已经过验证并且是正确的。从文件中删除后引发错误的特定行将在其他一些.yml文件中给出错误。正在使用当前版本的gemi18n(0.9.5)。Rails服务器启动但加载Web应用程序第一页时因同样的错误而中断。代码遇到i18n翻译代码时抛出错误,当错误从一个文件转移到另一个文件时,yml格式不是问题。

随机推荐