有人能给我一个node.js应用程序的例子吗

发布于 2024-09-04 09:24:47 字数 196 浏览 2 评论 0原文

我试图了解现有的一些较新的 Web 编程框架(即 Node.js、Rails 和 Sinatra)之间的差异。

有人能给我一个最适合每个框架的应用程序示例吗?

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

I'm trying to understand the differences between some of the newer web programming frameworks that now exists, namely Node.js, Rails, and Sinatra.

Could someone give me an example of applications that would work best on each of the frameworks?

That is to say, what is an application that would be best suited for Node.js as opposed to Rails or Sinatra and what is an application that is best suited for Rails as opposed to Node.js and Sinatra etc.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

佞臣 2024-09-11 09:24:47

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

非常不同。从本质上讲,node.js 是 V8 和事件库以及面向事件的标准库的组合。与 Ruby 的 EventMachine 相比,node.js 更好。

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

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 拥有整个围绕事件概念构建的标准库,这意味着它更适合任何问题即 I/O 限制。一个很好的例子是聊天应用程序

Sinatra 和 Rails 都是非常精致、稳定且流行的 Web 框架。 Node.js 有一些 Web 框架,但目前没有一个达到其中任何一个的质量。

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

Sinatra and Rails are both web frameworks. They provide common web development abstractions, such as routing, templating, file serving, etc.

node.js is very different. At it's core, node.js is a combination of V8 and event libraries, along with an event-oriented standard library. node.js is better compared to EventMachine for Ruby.

For example, here's an event-based HTTP server, using 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
}

And here's a node.js example:

var http = require('http');

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

The benefit of this approach is that the server doesn't block on each request (they can be processed in parallel)!

node.js has its whole standard library built around the concept of events meaning that it's much better suited to any problem that is I/O bound. A good example would be a chat application.

Sinatra and Rails are both very refined, stable and popular web frameworks. node.js has a few web frameworks but none of them are at the quality of either of those at the moment.

Out of the choices, if I needed a more stable web application, I'd go for either Sinatra or Rails. If I needed something more highly scalable and/or diverse, I'd go for node.js

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文