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