Phần 13: NodeJS – Web Module – NodeJS căn bản cho người mới bắt đầu

Phần  13: NodeJS – Web Module                        – NodeJS căn bản cho người mới bắt đầu

Bài 13: NodeJS – Web Module
– NodeJS căn bản cho người mới bắt đầu

 

Web Server là gì?

Web Server là 1 ứng dụng phần mềm có thể xử lý các HTTP request được gởi do HTTP Client (ví dụ: trình duyệt) và trả về 1 website trong phản hồi đến Client. Web Server thường gởi những tài liệu html bên cạnh các hình ảnh cũng như là type sheet và các đoạn Javascript.

Cấu trúc ứng dụng Web

1 ứng dụng internet thường được chia làm 4 lớp như dưới đây:
Client – Lớp này gồm các trình duyệt và những ứng dụng có thể tạo nên các HTTP request tới Web Server.
Server – Lớp này gồm các Web Server có thể can thiệp các request được tạo do Client và trả về những phản hồi (response).
Business – Lớp này gồm ứng dụng trên Server có thể được tận dụng do các Web Server để thực hiện các tiến trình xử lý cần thiết. Lớp này tương tác với lớp Data qua những chương trình bên ngoài.
Data – Lớp này gồm các Database và bất cứ những nguồn dữ liệu nào.

Tạo Web Server do sử dụng Node.js

Node.js cung cấp http Module có thể được dùng để tạo các HTTP consumer và server. Dưới đây chính là phần kiến trúc thu nhỏ của HTTP Server được lắng nghe trên cổng 8081.
Đầu tiên, bạn tạo server.js có nội dung như dưới đây:
var http = require('http');
var fs = require('fs');
var url = require('url');

// Create a server
http.createServer( perform (request, response) {  
   // Parse the request containing file identify
   var pathname = url.parse(request.url).pathname;
   
   // Print the identify of the file for which request is made.
   console.log("Request for " + pathname + " obtained.");
   
   // Read the requested file content material from file system
   fs.readFile(pathname.substr(1), perform (err, knowledge) {
      if (err) {
         console.log(err);
         
         // HTTP Status: 404 : NOT FOUND
         // Content Type: textual content/plain
         response.writeHead(404, 'Content-Type': 'textual content/html');
      } else {	
         //Page discovered	  
         // HTTP Status: 200 : OK
         // Content Type: textual content/plain
         response.writeHead(200, 'Content-Type': 'textual content/html');	
         
         // Write the content material of the file to response physique
         response.write(knowledge.toString());		
      }
      
      // Send the response physique 
      response.finish();
   });   
}).hear(8081);

// Console will print the message
console.log('Server working at http://127.0.0.1:8081/');
Hiện tại tạo index.htm cùng folder với server.js:
&lthtml&gt
   &lthead&gt
      &lttitle&gtSample Page&lt/title&gt
   &lt/head&gt
   
   &ltbody&gt
      Hello World!
   &lt/physique&gt
&lt/html&gt
Chạy server.js để xem kết quả:
$ node server.js
Kiểm tra kết quả
Server working at http://127.0.0.1:8081/
Tạo Request đến Node.js Server
Mở http://127.0.0.1:8081/index.htm trong bất kỳ trình duyệt nào và xem kết quả.
Kiểm tra kết quả:
Server working at http://127.0.0.1:8081/
Request for /index.htm obtained.

Tạo Web Client do sử dụng Node.js

Để tạo Web Client, bạn cũng sẽ có thể sử dụng http Module. Dưới đây chính là ví dụ minh họa.
Tạo consumer.js có nội dung sau:
var http = require('http');

// Options for use by request 
var choices = {
   host: 'localhost',
   port: '8081',
   path: '/index.htm'  
};

// Callback perform is used to take care of response
var callback = perform(response) {
   // Continuously replace stream with knowledge
   var physique = '';
   response.on('knowledge', perform(knowledge) );
   
   response.on('finish', perform() 
      // Data obtained fully.
      console.log(physique);
   );
}
// Make a request to the server
var req = http.request(choices, callback);
req.finish();
Chạy consumer.js trên 1 màn hình Terminal khác để xem kết quả:
$ node consumer.js
Kiểm tra kết quả:
&lthtml&gt
   &lthead&gt
      &lttitle&gtSample Page&lt/title&gt
   &lt/head&gt
   
   &ltbody&gt
      Hello World!
   &lt/physique&gt
&lt/html&gt
Tiếp đó kiểm tra kết quả trên Server.
Server working at http://127.0.0.1:8081/

 

admin

Leave a Reply

Your email address will not be published. Required fields are marked *