Could someone give me an example of node.js application

后端 未结 1 1691
醉酒成梦
醉酒成梦 2021-01-02 17:20

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 giv

相关标签:
1条回答
  • 2021-01-02 18:21

    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

    0 讨论(0)
提交回复
热议问题