Why and when to use Node.js? [duplicate]

2019-01-09 23:05发布

问题:

Possible Duplicate:
How to decide when to use Node.js?

Sorry if I'm a bit ambiguous, but I'm trying to understand the real advantages of using Node.js instead of other server-side language.

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.

回答1:

It's evented asynchronous non-blocking I/O build 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).

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.

It gives you useful tools like creating a HTTP server, creating a TCP server, handling file 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.

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.

There are plenty of OS libraries for Node.js that will give you abstractions, like Express.js and 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.



回答2:

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.

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.



回答3:

When you're (or even if you are not) a JavaScript enthusiast you can/should use Node.js for a number of reasons:

  • 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 modules and the npm package 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.


回答4:

Personally, I'd most likely use Node.js when:

  • 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).