问题描述
我是这种东西的新手,但最近我听到了很多关于 Node.js 是.考虑到我通常非常喜欢使用 jQuery 和 JavaScript,我不禁想知道如何决定何时使用 Node.js.我想到的 Web 应用程序类似于 Bitly - 获取一些内容,将其存档.
I am new to this kind of stuff, but lately I've been hearing a lot about how good Node.js is. Considering how much I love working with jQuery and JavaScript in general, I can't help but wonder how to decide when to use Node.js. The web application I have in mind is something like Bitly - takes some content, archives it.
从我这几天做的所有作业中,我获得了以下信息.节点.js
From all the homework I have been doing in the last few days, I obtained the following information. Node.js
- 是一个命令行工具,可以作为一个普通的网络服务器运行,并允许运行 JavaScript 程序
- 利用出色的 V8 JavaScript 引擎
- 当你需要同时做几件事时非常好
- 是基于事件的,所以所有美妙的Ajax之类的东西都可以在服务器端完成
- 让我们在浏览器和后端之间共享代码
- 让我们谈谈 MySQL
- is a command-line tool that can be run as a regular web server and lets one run JavaScript programs
- utilizes the great V8 JavaScript engine
- is very good when you need to do several things at the same time
- is event-based so all the wonderful Ajax-like stuff can be done on the server side
- lets us share code between the browser and the backend
- lets us talk with MySQL
我遇到的一些来源是:
考虑到 Node.js 几乎可以在 亚马逊的 EC2 上开箱即用 实例,我试图了解什么类型的问题需要 Node.js,而不是像 PHP、Python 和 Ruby.我知道这真的取决于一个人对一种语言的专业知识,但我的问题更多地属于以下一般类别:何时使用特定框架以及它特别适合什么类型的问题?
Considering that Node.js can be run almost out-of-the-box on Amazon's EC2 instances, I am trying to understand what type of problems require Node.js as opposed to any of the mighty kings out there like PHP, Python and Ruby. I understand that it really depends on the expertise one has on a language, but my question falls more into the general category of: When to use a particular framework and what type of problems is it particularly suited for?
推荐答案
您在总结 Node.js 的精彩之处方面做得非常出色.我的感觉是 Node.js 特别适合您希望保持从浏览器到服务器的持久连接的应用程序.使用称为 "long-polling" 的技术,您可以编写发送更新的应用程序实时呈现给用户.对许多网络巨头进行长时间轮询,例如 Ruby on Rails 或 Django 会在服务器上产生巨大的负载,因为每个活动的客户端都会占用一个服务器进程.这种情况相当于 tarpit 攻击.当你使用 Node.js 之类的东西时,服务器不需要为每个打开的连接维护单独的线程.
You did a great job of summarizing what's awesome about Node.js. My feeling is that Node.js is especially suited for applications where you'd like to maintain a persistent connection from the browser back to the server. Using a technique known as "long-polling", you can write an application that sends updates to the user in real time. Doing long polling on many of the web's giants, like Ruby on Rails or Django, would create immense load on the server, because each active client eats up one server process. This situation amounts to a tarpit attack. When you use something like Node.js, the server has no need of maintaining separate threads for each open connection.
这意味着您可以在 Node.js 中创建一个几乎不需要系统的基于浏览器的聊天应用程序资源服务于众多客户.任何时候你想要进行这种长轮询,Node.js 都是一个不错的选择.
This means you can create a browser-based chat application in Node.js that takes almost no system resources to serve a great many clients. Any time you want to do this sort of long-polling, Node.js is a great option.
值得一提的是,Ruby 和 Python 都有执行此类操作的工具(eventmachine 和 twisted,分别),但是 Node.js 从头开始做得非常好.JavaScript 非常适合基于回调的并发模型,它在这方面表现出色.此外,能够使用客户端和服务器本地的 JSON 进行序列化和反序列化非常漂亮.
It's worth mentioning that Ruby and Python both have tools to do this sort of thing (eventmachine and twisted, respectively), but that Node.js does it exceptionally well, and from the ground up. JavaScript is exceptionally well situated to a callback-based concurrency model, and it excels here. Also, being able to serialize and deserialize with JSON native to both the client and the server is pretty nifty.
我期待在这里阅读其他答案,这是一个很棒的问题.
I look forward to reading other answers here, this is a fantastic question.
值得指出的是,Node.js 也非常适合在客户端/服务器之间重用大量代码的情况.Meteor 框架 让这一切变得非常简单,而且很多人都认为这可能是 Web 开发的未来.我可以从经验中说,在 Meteor 中编写代码非常有趣,其中很大一部分是花更少的时间思考如何重构数据,因此在浏览器中运行的代码可以轻松操纵它并将其传回.
It's worth pointing out that Node.js is also great for situations in which you'll be reusing a lot of code across the client/server gap. The Meteor framework makes this really easy, and a lot of folks are suggesting this might be the future of web development. I can say from experience that it's a whole lot of fun to write code in Meteor, and a big part of this is spending less time thinking about how you're going to restructure your data, so the code that runs in the browser can easily manipulate it and pass it back.
这是一篇关于 Pyramid 和长轮询的文章,结果证明在 gevent 的帮助下很容易设置:TicTacToe 和金字塔长轮询.
Here's an article on Pyramid and long-polling, which turns out to be very easy to set up with a little help from gevent: TicTacToe and Long Polling with Pyramid.
这篇关于如何决定何时使用 Node.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!