Translator: cherryvenus

The original link

There are many reasons to use Node.js, regardless of your experience. Let’s take a look at some of the main reasons to use Node and why you might love it.

Ah! I understand. You’re not a bandwagon developer. Because no one else does, you don’t use some cool, up-to-date platform. That’s why you haven’t taken Node.js seriously (or your boss hasn’t let you do it yet). Well, it’s time to take a fresh look at Node.js. There are many reasons to use Nodejs, and here are 10 of them.

1. You are familiar with JavaScript

Let me guess. You are using a powerful client-side framework (Angular, Ember, Backbone) and a Rest-ful server-side API to transfer and receive data using JSON as a data format. Even if you don’t use any of these frameworks, you’ve written your own in jQuery. So if you’re not using Node.js on the server, keep translating. You’re translating two things: 1) the logic from JavaScript in your head into server-side framework logic. And 2) convert HTTP data from JSON to objects on your server side.

With JavaScript running through your application, not only do you gain intelligence, but you also gain hands-on experience. By reusing models and templates, you can reduce the size of your application while reducing complexity and the possibility of bugs.

JavaScript as a language is taking over the world! Pretty soon, every computer in the world will have a JavaScript environment running on it, and it looks like it will be for a while.

2. It is fast

Node.js is a JavaScript runtime environment that uses Google’s V8 engine in Chrome. V8 compiles and executes JavaScript at lightning speed because V8 compiles JavaScript to native machine code.

Besides making JavaScript run at lightning speed, the real magic of Node.js is the Event loop. The Event loop is single-threaded in all asynchronous I/O operations. Traditionally, I/O operations can run synchronously (blocking) or asynchronously, with parallel threads performing the work. This traditional approach consumes a lot of memory and is notoriously hard to program. By contrast, when a Node application needs to perform an I/O operation, it passes an asynchronous task to the Event loop, adds a callback function, and proceeds with the rest of the program. When the asynchronous operation is complete, the Event loop returns the task to execute its callback function.

In other words, network connection reads and writes, file system reads and writes, and database reads and writes — all the everyday tasks in web applications — are performed very, very quickly in Node. Node allows you to create fast, large-scale web applications that can handle high throughput and large numbers of real-time connections.

3. The tool

NPM is the package manager for Node.js and it’s awesome. This is great, of course, similar to package managers in other ecosystems, but NPM is fast, steady and consistent. He does a very good job of finding and installing project dependencies. He isolates packages from other projects to prevent version confusion. However, it also handles global shell command installations and platform-dependent binaries. For a while, I can’t remember the packages I installed in NPM, and here I have to ask myself, “Why are these modules confused? Where is the module installed? Why this version and not the other?”

Grunt is an older task manager, but there’s a new generation of tools like Gulp, Brunch, and Broccoli that focus on creating and compiling your files, taking advantage of JavaScript’s powerful file streaming capabilities.

4. Once again you refresh your knowledge of JavaScript

So you’ve decided to use JavaScript on your server, and you’re grateful for your decision to avoid all compiling from client-side data to server-side data. But data to the database requires even more compilation!

There’s good news. If you’re using an object database like Mongo, then you can extend JavaScript to the persistence layer as well. This is good news. If you’re using an object database like Mongo, then you can extend JavaScript to make the persistence layer even better.

Using Node.js allows you to use the same language on the client, server, and database. From browser to disk, you can save your data in its native JSON format.

5. Simplify in real time

If Node.js is good at multiple concurrent connections, it’s also good at multi-user, real-time web applications such as chat rooms and games. Node event loop can meet the requirements of multiple users. Real-time capability uses the WebSocket protocol. Websocket is a simple two-way communication channel between client and server. So the server can push data to the client just like the client pushes it. Websocket runs over TCP, avoiding HTTP consumption.

Socket. IO is a popular library in the Websoket library that makes it easy to kill collaborative Web applications. Here is a simple server using socket. IO:

var app = require('http').createServer(handler)
var io = require('socket.io')(app);

app.listen(8080);

io.on('connection', function (socket) {

  // Send a message to the client
  socket.emit('event to client', { hello: 'world' });

  // Handle a message from the client
  socket.on('event from client, function (data) {
    console.log(data);
  });
});

Copy the code

6. The flow data

Traditionally, Web frameworks treat HTTP requests and responses as a monolithic data object. In effect, they are I/O streams, which you can imagine if you take files out of the file system as streams. Since Node.js is very good at handling I/O, we can use this feature to create some cool things. For example, it can transcode audio or video files as they are uploaded, reducing the overall processing time.

Node can read and write streams to Websocket just as it can read and write streams to HTTP. For example, we can send webSocket standard output from a process running in the server to the browser and display the output in real time on a web page.

7. A codebase and free real-time

If you already know this much, you might be asking yourself, “If Node.js allows you to use JavaScript on both the server and the client, and makes it easy to transfer data between the client and the server, can I write a Web application with a code base that runs on both the client and the server, And processing data automatically and asynchronously between the two.”

The answer to that question is yes, and the framework for this app could be Meteor. Meteor is the next generation Node-based Web framework. He can run the same code base on both the client and server sides. The data is then automatically saved to the server. Other ways work too! Any data on the server is automatically transferred to the client. The future will be better! Any web page that displays data will automatically respond and automatically update.

// Save the value of 'name' upon clicking 'submit' directly in the browser!
'.click .submit': function(e, tpl) {
  Users.update(
    { _id: this._id },
    { $set: { name: $('.name').val() }}
  );
}

Copy the code

8. Business managers

The potential risk of any open source project is that it is abandoned by the project maintainers. This is not the current state of Node.js. Node is now funded by Joyent, which employs the project leader and other core contributors, so there is a real company supporting the future of the project. Not to mention a number of large companies, including Walmart, Microsoft, Yahoo, Paypal, Voxer, and many more, support the program at every level.

9. Escrow

With the rapid adoption of Node.js, the world class scale of Node.js hosting is also growing. In particular, platform-as-a-service (PaaS) providers, such as Modulus and other deployments reduced to one command. The granddaddy of PaaS, Heroku, now officially supports Node deployment.

10. Every developer knows (some) JavaScript

This is the reason to give you the boss.

With the rise of the Web, JavaScript clicks and mouse-over events already exist. Every Web developer has written some JavaScript at some point, even JavaScript developed using the jQuery plug-in. Finding a web developer savvy is hard right now, so when choosing a Web platform, why not choose a language that every Web developer in the world is familiar with?

Conclusion! The reward!

But wait, there’s more! As with any platform or product, open source or otherwise, its community has a huge impact. Node is second to none in this regard. From parties to conferences, there are a lot of really smart people working on the ecosphere every day. At the same time, the community is very welcome. These smart people are happy to help these newcomers with Node, even with general programming. At IRC, you don’t have to be shy about asking questions or opening up a topic. The community is very active, with over 91000 modules on NPM. And the community is generous. In 2013, more than $70,000 in private sponsorship helped public NPM servers run.

Yes, Node is popular right now. This is Web development, so maybe next week, Node will disappear and the next hot spot will appear (will it be GO or Elixir?). . But try!