Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

javascript - 如何决定何时使用Node.js?(How to decide when to use Node.js?)

I am new to this kind of stuff, but lately I've been hearing a lot about how good Node.js is.

(我对这种东西是陌生的,但是最近我已经听到很多关于Node.js有多出色的信息。)

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.

(考虑到我一般喜欢使用jQuery和JavaScript有多少,我不禁想知道如何决定何时使用Node.js。)

The web application I have in mind is something like Bitly - takes some content, archives it.

(我想到的Web应用程序有点像Bitly-需要一些内容,然后将其存档。)

From all the homework I have been doing in the last few days, I obtained the following information.

(从过去几天我所做的所有作业中,我获得了以下信息。)

Node.js

(Node.js)

  • is a command-line tool that can be run as a regular web server and lets one run JavaScript programs

    (是一种命令行工具,可以作为常规Web服务器运行,并且可以运行一个JavaScript程序)

  • utilizes the great V8 JavaScript engine

    (利用强大的V8 JavaScript引擎)

  • 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

    (是基于事件的,因此所有出色的类似于Ajax的工作都可以在服务器端完成)

  • lets us share code between the browser and the backend

    (让我们在浏览器和后端之间共享代码)

  • lets us talk with MySQL

    (让我们与MySQL对话)

Some of the sources that I have come across are:

(我遇到的一些来源是:)

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 .

(考虑到Node.js几乎可以直接在亚马逊的EC2实例上运行,我试图了解Node.js需要哪种类型的问题,而不是像PHPPythonRuby这样的强大之王。)

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?

(我知道这确实取决于一个人在某种语言上的专业知识,但是我的问题更多地归结为以下一般类别:什么时候使用特定框架,它特别适合什么类型的问题?)

  ask by Legend translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You did a great job of summarizing what's awesome about Node.js.

(在总结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.

(我的感觉是,Node.js特别适用于您想要维持从浏览器到服务器的持久连接的应用程序。)

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.

(在Ruby on RailsDjango之类的许多Web巨人上进行长时间的轮询会在服务器上产生巨大的负载,因为每个活动的客户端都要吃掉一个服务器进程。)

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之类的服务器时,服务器无需为每个打开的连接维护单独的线程。)

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.

(这意味着您可以在Node.js中创建一个基于浏览器的聊天应用程序 ,该应用程序几乎不需要占用系统资源即可为大量客户端提供服务。)

Any time you want to do this sort of long-polling, Node.js is a great option.

(每当您想进行这种长轮询时,Node.js都是一个不错的选择。)

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.

(值得一提的是,Ruby和Python都具有执行此类操作的工具(分别是eventmachinetwisted ),但是Node.js从头开始就表现出色。)

JavaScript is exceptionally well situated to a callback-based concurrency model, and it excels here.

(JavaScript在基于回调的并发模型中处于特殊位置,并且在这里很有用。)

Also, being able to serialize and deserialize with JSON native to both the client and the server is pretty nifty.

(而且,能够使用客户端和服务器都原生的JSON进行序列化和反序列化非常漂亮。)

I look forward to reading other answers here, this is a fantastic question.

(我期待在这里阅读其他答案,这是一个很棒的问题。)

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.

(值得指出的是,Node.js对于在客户端/服务器之间重复使用大量代码的情况也非常有用。)

The Meteor framework makes this really easy, and a lot of folks are suggesting this might be the future of web development.

(Meteor框架使这变得非常容易,并且很多人都认为这可能是Web开发的未来。)

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.

(我可以从经验中说,用Meteor编写代码非常有趣,其中很大一部分是花更少的时间思考如何重组数据,因此可以轻松地在浏览器中运行代码操纵它,并将其传递回去。)

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 .

(这是一篇有关金字塔和长轮询的文章,事实证明,在gevent的一点帮助下,设置起来非常容易: TicTacToe和金字塔长轮询 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...