如何决定何时使用Node.js? [英] How to decide when to use Node.js?

查看:167
本文介绍了如何决定何时使用Node.js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这样的东西的新手,但最近我听到很多关于 Node.js 是。考虑到我喜欢与jQuery和JavaScript一起工作,我不禁想知道如何决定何时使用Node.js。我想到的网络应用程序是像 Bitly - 需要一些内容,归档它。



在过去几天我一直在做的所有功课中,我获得了以下信息。 Node.js




  • 是一个可以作为常规Web服务器运行的命令行工具,可让您运行JavaScript程序

  • 利用伟大的 V8 JavaScript引擎

  • 非常好,当你需要同时做几件事情

  • 是基于事件的,所以所有的精彩 Ajax 类似的东西可以在服务器端完成

  • 让我们分享浏览器和后端之间的代码

  • 让我们与MySQL交谈



我遇到的是:





考虑到Node.js可以几乎运行开箱即用的亚马逊的EC2 实例,我试图了解什么类型的问题需要Node.js,而不是任何强大的国王,如 PHP Python ,rel =nofollow noreferrer>。我明白,这真的取决于对一种语言的专业知识,但是我的问题更多地归入以下一般类别:何时使用特定的框架,特别适合什么类型的问题?

你做了一个很好的工作,总结什么是令人敬畏的Node.js.我的感觉是,Node.js特别适用于您希望维护从浏览器到服务器的持久连接的应用程序。使用一种称为长轮询的技术,您可以编写发送更新的应用程序给用户实时。对许多网络巨头进行长时间的投票,例如 Ruby on Rails Django ,将在服务器上创建巨大的负载,因为每个活动的客户端都会占用一个服务器进程。这种情况相当于 tarpit 攻击。当您使用像Node.js这样的东西时,服务器不需要为每个打开的连接维护单独的线程。



这意味着您可以创建一个基于浏览器的聊天应用程序在Node.js中,几乎没有系统资源来服务于很多客户端。任何时候你想做这种长时间轮询,Node.js是一个很好的选择。值得一提的是,Ruby和Python都有工具来做这样的事情( eventmachine twisted ),但是Node.js做得非常好,从头开始。 JavaScript非常适合基于回调的并发模型,而且在这里非常出色。此外,能够使用JSON本机对客户端和服务器进行序列化和反序列化都是非常漂亮的。



我期待着在这里阅读其他答案,这是一个奇妙的问题。值得一提的是,Node.js对于在客户机/服务器间隙中重用很多代码的情况也是非常有用的。 流星框架使这很简单,很多人都认为这可能是Web开发的未来。从经验可以说,在Meteor中编写代码是非常有趣的,其中很大一部分是花费更少的时间思考如何重组数据,因此在浏览器中运行的代码很容易操纵它并传回它。



这里有一篇关于金字塔和长时间轮询的文章,其原理很容易由gevent提供一些帮助: TicTacToe和使用金字塔的长轮询


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.

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

  • 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

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. 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?

解决方案

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.

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.

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.

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.

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆