【问题标题】:Could someone give me an example of node.js application有人可以给我一个 node.js 应用程序的例子吗
【发布时间】:2011-02-27 18:43:51
【问题描述】:

我正在尝试了解现在存在的一些较新的 Web 编程框架之间的区别,即 Node.js、Rails 和 Sinatra。

谁能给我一个在每个框架上运行最好的应用程序示例?

也就是说,与 Rails 或 Sinatra 相比,最适合 Node.js 的应用程序是什么,与 Node.js 和 Sinatra 等相比,最适合 Rails 的应用程序是什么... ..

【问题讨论】:

    标签: ruby-on-rails sinatra node.js


    【解决方案1】:

    Sinatra 和 Rails 都是 Web 框架。它们提供常见的 Web 开发抽象,例如路由、模板、文件服务等。

    node.js 非常不同。 node.js 的核心是 V8 和事件库的组合,以及面向事件的标准库。 node.js 比 EventMachine for Ruby 更好。

    例如,这是一个基于事件的 HTTP 服务器,使用 EventMachine:

    require 'eventmachine'
    require 'evma_httpserver'
    
    class MyHttpServer < EM::Connection
      include EM::HttpServer
    
      def post_init
        super
        no_environment_strings
      end
    
      def process_http_request
        response = EM::DelegatedHttpResponse.new(self)
        response.status = 200
        response.content_type 'text/plain'
        response.content = 'Hello world'
        response.send_response
      end
    end
    
    EM.run{
      EM.start_server '0.0.0.0', 8080, MyHttpServer
    }
    

    这是一个 node.js 示例:

    var http = require('http');
    
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello world');
    }).listen(8000);
    

    这种方法的好处是服务器不会阻塞每个请求(它们可以并行处理)!

    node.js 拥有完整的standard library built around the concept of events,这意味着它更适合任何受 I/O 限制的问题。 chat application 就是一个很好的例子。

    Sinatra 和 Rails 都是非常精致、稳定和流行的 Web 框架。 node.js 有一些 Web 框架,但目前它们的质量都不如其中任何一个。

    如果我需要一个更稳定的 Web 应用程序,我会选择 Sinatra 或 Rails。如果我需要更高度可扩展和/或多样化的东西,我会选择 node.js

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      相关资源
      最近更新 更多