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:
1 2 3 4 5 6 7 8 9 10 | $ me = new Person ('Tu dep zai'); $ ngoc_trinh = NguoiMau :: find ('ngoc_tring'); // First find Ngoc Trinh $ phone_number = $ ngoc_trinh-> phone_number; // After finding Ngoc Trinh, get her phone number $ dong_y = $ me-> goi_dien ($ phone_number); // Next call Ngoc Trinh and ask if she wants to go home to rest? if ($ dong_y) {// If she agrees, take her home to rest $ me.goToHotelWith ($ ngoc_trinh); } else {// If not, it will be at the Qwerty house. $ me-> oNhaQwerty (); } raw view |
It is easy to see things happening sequentially from top to bottom.
But for Node.js it is different: (Event every where)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | var me = new Person ('Tu dep zai'); NguoiMau.find ('ngoc_trinh', function (ngoc_trinh) {// ngoc_trinh this is Ngoc Trinh that we need to find var phone_number = ngoc_trinh.phone_number; me.goiDien (phone_number, function (dong_y) {// After waiting for Ngoc Trinh to decide we will receive Ngoc Trinh's decision if (dong_y) {// If she agrees, take her home to rest me.goToHotelWith (ngoc_trinh); } else {// If not, it will be at the Qwerty house. me.oNhaQwerty (); } }); // While waiting for Ngoc Trinh's decision, we can still sit Qwerty. me.oNhaQwerty (); // As a result, we can just wait for the phone and the hand can still exercise. }); me.hi ('Hello world'); // While I was looking for Ngoc Trinh, I could still greet the world :)) |
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:
1 2 3 4 5 6 7 8 9 10 11 12 | var me = new Person ('Tu dep zai'); me.diTam (function () {// After showering me.goiDienChoBanGai (function () {// After calling me.denNhaBanGai (function () {// After coming to my girlfriend's house me.duaBanGaiDiView Movie (function () {// After watching the movie me.duaBanGaiVeNhaDeNghi (function () {// After sending a girl home to rest me.veNha (); // Come home finally }); }); }); }); }); |
To avoid this we can use the following code code method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | function bat_dau () { me.diTam (tamXong); } function tamXong () { me.goiDienChoBanGai (goiDienXong); } function goiDienXong () { me.denNhaBanGai (denNhaBanGaiXong); } function denNhaBanGaiXong () { me.duaBanGaiDiView Movie (diViewPhimXong); } function diView MovieMong () { me.duaBanGaiVeNhaDeNghi (duaBanGaiVeNhaXong); } function duaBanGaiVeNhaXong () { me.veNha (); me.say ('Tired value!'); } start(); |
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):
1 2 3 4 5 6 7 8 | var express = require ('express'); var app = express (); var http = require ('http'). Server (app); http.listen (2389, function () { console.log ('Listening on localhost: 2389'); }); raw view |
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