How to Decide When to Use Node.Js

How to decide when to use 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.

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.

Why and when to use Node.js?

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.

How to decide which framework to use for node?

I'll try and keep this answer as non-opinionated as possible. Please edit and help me improve this. This is a important topic and should get a good answer.

Express.js

Pros

  • Express.js is the big guy, fairly old and incredibly popular.
  • Easy to use views
  • Very Lightweight

Cons

  • No fancy features

Koa.js

Pros

  • Lightweight
  • Koa is the maybe successor to Express
  • No callback hell, thanks to ES6 generators
  • Built by the same guy as Express

Cons

  • Fairly new, not super refined
  • Uses ECMAScript 6 features, meaning you need Node.js v0.11 (unstable)

Hapi

Pros

  • Many official modules, but not a ton of third-party ones
  • Developed by a large corporation that uses it for their own products

Cons

  • Built by Walmart
  • Different syntax for specifying routes than Koa or Express.
  • Itself and all the official modules have weird names

Synth

The first back-end framework specially designed for single-page web applications.

Pros

  • Designed for being the backend single-page websites, ala Angular.js
  • API First
  • Dependency Injection, familiar to those coming from Java
  • Designed to do a ton of the backend things, allowing you to work on the frontend

Cons

  • Dependency Injection, disliked by quite a lot of people because Java is the only reason it exists
  • Very new, currently in beta
  • Doesn't work well with more traditional websites

More Resources

  • TechEmpower Framework Benchmarks (limit to Node.js) Benchmarks of lots of web frameworks, currently only has raw Node.js, Express, and Hapi for Node.js.

Which version of Node.js should I choose

Node 0.10.0 is relatively new and many frameworks and libraries do not support this version yet.

Node 0.9.x was an unstable release as every odd-numbered version (every even numbered version is considered stable, e.g., 0.6.x, 0.8.x, 0.10.x, ...).

For starting you might use the latest 0.8.x version and upgrade to 0.10.x later as libraries evolve.

The node versioning schema may be found here: https://github.com/joyent/node/wiki/FAQ#what-is-the-versioning-scheme

Hint: There is the tool Nave which allows you to use several node installations at once.

Edit 2013-12-04

Meanwhile Node 0.10.x is very stable and should be chosen.

How to connect a user to wifi with nodejs

I found that the server cannot ask to connect a smartphone to a network.
It is a protected function at the operating system level.
Therefore this function cannot be performed.



Related Topics



Leave a reply



Submit