Feeling for the first time experiencing Node.js

As heaven says, Node.js is a platform built on Google Chrome V8 's Google Chrome V8 compiler – don't ask. This compiler is very fast on the browser, so people think of how to put it on the server side . So Node.js was born.

When I first heard of Node.js running asynchronously or called non-blocking, it sounded very dangerous, but in reality it was normal :))

Run asynchronously

If you know PHP you will easily understand this example:

It is easy to see things happening sequentially from top to bottom.

But for Node.js it is different: (Event every where)

At this time, it is like having fuselage to do a lot of things at the same time. Each task that has to wait for it will create an event, the event that will callback function will be run. As in the example while you wait for Ms Ngoc Trinh to decide, you can still sit oQQQtyty (). That makes Node.js run faster and more optimized.

If you've ever used JQuery , you know Ajax . While ajax has callback functions like complete (), success (), done () to call when a request is completed or successful or something like that.

So what if I want to sync in Node.js ?
Then in the corresponding callback function we call the next job! At first I was quite confused with this. If there are so many nested callbacks, it is very easy to call call hell hell . Or more simply, using async is done.

For example:

To avoid this we can use the following code code method:

Very clean and dry, right :))

There is another way that they think of in ES6 that is Promise . In this article I will not talk about it because it is quite complicated.

PHP has composer, Node.js has npm

npm is a Node.js package manager, it is a project that helps NodeJS grow as fast as it is now.

When adding a new library to the project, you just need to download the library via the command:

npm install --save package_name

Then import in by:

var fs = require('fs');//fs là 1 package xử lý về file.

Creating a package and uploading it to npmjs.com is very simple, so more and more packages are created every day. But it simply means that there are many garbage packages created, so when using packages, if it has multiple downloads on npm, is there any star on GitHub? Then decide whether to use or find another guy.

Node.js creates RESTful API very well

It is Javascript, json from javascript. Only with a package like express can I create a RESTful API very quickly but effectively.

Node.js create realtime application, then extremely critical

Simply with a package like socket.io I can create a realtime chat application very fast and easy. Because Node.js directs the event, it can create realtime applications that are easy to understand. In another post, I will guide you to create a chat application with socket.io.

Node.js does not create many processes

Each running Node.js will create a unique process. In contrast to PHP, each user's incoming request will create a process for processing. This helps Node.js save more system resources.

How to create a server using Node.js

Node.js doesn't need a server like Apache or Nginx at all, it can create the server itself. You can see the following example (server.js):

Then run with the command:

node server.js

It will create a server and listen at port 2389. So to run the application on the real server you must have at least 1 VPS to be able to run the terminal but can not use hosting like PHP applications for example. Since it is a fairly new technology, there are very few server or hosting providers that support Node.js application. If you want to test the Node.js application, think about Heroku or Openshift .

To manage the node.js app running on the server I usually use pm2 of Keymetric . You can register for an account there and then connect the server to keymetrics and can manage it right on the Web application.

Each Node.js app will listen at one port, so how about running multiple node.js apps on the same server that is on port 80?
At that time, Node.js had to ask for help from Apache or Nginx to create virtual hosts and then use the proxy to map between virtual host and your Node.js app.

It's a long time, okay! If you love Node.js please follow the blog in the next post ?

ITZone via blogk.xyz

Share the news now