Javascript 为什么以及何时使用 Node.js?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5617683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why and when to use Node.js?
提问by gion_13
Possible Duplicate:
How to decide when to use Node.js?
可能的重复:
如何决定何时使用 Node.js?
Sorry if I'm a bit ambiguous, but I'm trying to understand the real advantages of using Node.jsinstead of other server-side language.
对不起,如果我有点模棱两可,但我试图了解使用Node.js而不是其他服务器端语言的真正优势。
I'm a JavaScript enthusiast, so I'm probably going to play with Node.js, but I want to know if I should use it in my projects.
我是一个 JavaScript 爱好者,所以我可能会使用 Node.js,但我想知道我是否应该在我的项目中使用它。
采纳答案by Raynos
It's evented asynchronous non-blocking I/Obuild ontop of V8.
So we have all the performance gain of V8 which is the Google JavaScript interpreter. Since the JavaScript performance race hasn't ended yet, you can expect Google to constantly update performance on V8 (for free).
所以我们拥有 V8 的所有性能增益,它是 Google JavaScript 解释器。由于 JavaScript 性能竞赛尚未结束,您可以期待 Google 不断更新 V8 上的性能(免费)。
We have non-blocking I/O which is simply the correct way to do I/O. This is based on an event loop and using asynchronous callbacks for your I/O.
我们有非阻塞 I/O,这是进行 I/O 的正确方法。这是基于事件循环并为您的 I/O 使用异步回调。
It gives you useful tools like creating a HTTP server, creating a TCPserver, handling file I/O.
它为您提供了有用的工具,例如创建 HTTP 服务器、创建TCP服务器、处理文件 I/O。
It's a low level highly performant platform for doing any kind of I/O without having to write the entire thing in C from scratch. And it scales very well due to the non-blocking I/O.
它是一个低级的高性能平台,用于执行任何类型的 I/O,而无需从头开始用 C 编写整个内容。由于非阻塞 I/O,它可以很好地扩展。
So you want to use Node.js if you want to write highly scaling and efficient applications using non-blocking I/O whilst still having a high level scripting language available. If needed, you can hand optimise parts of your code by writing extensions in C.
因此,如果您想使用非阻塞 I/O 编写高度可扩展和高效的应用程序,同时仍然拥有可用的高级脚本语言,那么您想使用 Node.js。如果需要,您可以通过用 C 编写扩展来手动优化部分代码。
There are plenty of OS libraries for Node.js that will give you abstractions, like Express.jsand now.
有很多用于 Node.js 的操作系统库可以为您提供抽象,例如Express.js和now。
You don't want to use Node.js if you want (slow) high level abstractions to do everything for you. You don't want to use Node.js if you want RAD. You don't want to use Node.js if you can't afford to trust a young platform, either due to having to write large pieces of code yourself to do things that are build into other frameworks or because you can't use Node.js, because the API isn't stable yet or it's a sub 1.0 release.
如果您希望(缓慢的)高级抽象为您做所有事情,您就不想使用 Node.js。你不想,如果你想使用Node.js的RAD。如果你不能信任一个年轻的平台,你就不想使用 Node.js,要么是因为必须自己编写大量代码来做其他框架内置的事情,要么是因为你不能使用 Node .js,因为 API 还不稳定或者它是 1.0 版本。
回答by David Tang
The two most oft-quoted advantages are:
两个最常被引用的优点是:
- JavaScript is both server-side and client-side. There are fewer things to learn, less context switching, and the ability to reuse code across the two sides.
- Uses non-blocking I/O, and Chrome's V8 engine, to provide fast, highly scalable servers.
- JavaScript 既是服务器端又是客户端。需要学习的东西更少,上下文切换更少,并且能够在两端重用代码。
- 使用非阻塞 I/O 和 Chrome 的 V8 引擎,提供快速、高度可扩展的服务器。
For me though, the most interesting part is the amount of activity happening in this area. There are a lot of very interesting ideas under development for node - be sure to check out the list of Node.js modules.
不过对我来说,最有趣的部分是该领域发生的活动量。node 有很多非常有趣的想法正在开发中 - 请务必查看Node.js 模块列表。
回答by yojimbo87
When you're (or even if you are not) a JavaScript enthusiast you can/should use Node.js for a number of reasons:
当您是(或者即使您不是)JavaScript 爱好者时,出于多种原因,您可以/应该使用 Node.js:
- It's a low-level, lightweight and standalone framework which brings power of JavaScript to the server-side environment.
- If you like more higher level abstraction then there is a large number of modulesand the npmpackage manager where you can find wide range of ready-to-use applications.
- Fast/unencumbered development process - for example, you don't need tons of additional tools in order to start writing serious stuff.
- Big open source based community full of enthusiasts and very talented people.
- Made for creating real-time web oriented applications - that's where the (near) future is.
回答by Demian Brecht
Personally, I'd most likely use Node.js when:
就个人而言,我最有可能在以下情况下使用 Node.js:
- I want to write a server that doesn't use the HTTP protocol.
- I'm prototyping a server implementation.
- I'm writing a server that isn't expecting a ton of traffic (although I've never profiled a Node.js implementation next to, say, a matching C++ implementation).
- I want to get active in the community (which is apparently growing quite rapidly).
- 我想写一个不使用HTTP 协议的服务器。
- 我正在对服务器实现进行原型设计。
- 我正在编写一个不期望有大量流量的服务器(尽管我从未在匹配的 C++ 实现旁边分析过 Node.js 实现)。
- 我想在社区中活跃起来(显然社区增长很快)。