1. Trang chủ
  2. » Thể loại khác

web module trong nodejs

4 147 2

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

http://vietjack.com/nodejs/index.jsp                                                                                                              Copyright  ©  vietjack.com     Web Module Node.js Web Server ? Web Server ứng dụng phần mềm xử lý HTTP request gửi HTTP Client (ví dụ: trình duyệt web) trả trang web phản hồi tới Client Web Server thường gửi tài liệu html bên cạnh ảnh style sheet đoạn Javascript Cấu trúc ứng dụng Web Một ứng dụng web thường chia thành lớp sau: • Client - Lớp bao gồm trình duyệt web ứng dụng tạo HTTP request đến Web Server • Server - Lớp bao gồm Web Server can thiệp request tạo Client trả phản hồi (response) • Business - Lớp bao gồm ứng dụng Server tận dụng Web Server để thực tiến trình xử lý cần thiết Lớp tương tác với lớp Data qua chương trình bên ngồi http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/nodejs/index.jsp                                                                                                              Copyright â vietjack.com Data - Lp ny bao gồm Database nguồn liệu Tạo Web Server sử dụng Node.js Node.js cung cấp http Module sử dụng để tạo HTTP client server Dưới phần kiến trúc thu nhỏ HTTP Server lắng nghe cổng 8081 Đầu tiên, bạn tạo server.js có nội dung sau: var http = require('http'); var fs = require('fs'); var url = require('url'); // Tao mot Server http.createServer( function (request, response) { // Parse request co chua ten file var pathname = url.parse(request.url).pathname; // In thong tin ve ten file ma tu Request duoc tao console.log("Request cho " + pathname + " da duoc nhan."); // Doc noi dung tu File da duoc yeu cau boi Request fs.readFile(pathname.substr(1), function (err, data) { if (err) { console.log(err); // HTTP Status: 404 : NOT FOUND // Content Type: text/plain response.writeHead(404, {'Content-Type': 'text/html'}); }else{ // HTTP Status: 200 : OK // Content Type: text/plain response.writeHead(200, {'Content-Type': 'text/html'}); // Ghi noi dung cua File toi phan BODY cua Response response.write(data.toString()); } // Gui phan BODY cua Response response.end(); }); }).listen(8081); // In thong bao sau tren console console.log('Server dang chay tai dia chi: http://127.0.0.1:8081/'); Bây tạo index.htm thư mục với server.js: Vi du Web Module Node.js Hello World! Chạy server.js để xem kết quả: $ node server.js Kiểm tra kết Server dang chay tai dia chi: http://127.0.0.1:8081/ Tạo Request tới Node.js Server Mở http://127.0.0.1:8081/index.htm trình duyệt xem kết http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/nodejs/index.jsp                                                                                                              Copyright  ©  vietjack.com     Kiểm tra kết quả: Server dang chay tai dia chi: http://127.0.0.1:8081/ Request cho /index.htm da duoc nhan Tạo Web Client sử dụng Node.js Để tạo Web Client, bạn sử dụng http Module Dưới ví dụ minh họa Tạo client.js có nội dung sau: var http = require('http'); // Tao doi tuong options duoc su dung boi Request var options = { host: 'localhost', port: '8081', path: '/index.htm' }; // Ham callback duoc su dung de xu ly Response var callback = function(response){ // Tiep tuc cap nhat du lieu toi Stream var body = ''; response.on('data', function(data) { body += data; }); response.on('end', function() { // Ket thuc qua trinh nhan du lieu console.log(body); }); } // Tao mot Request toi Server var req = http.request(options, callback); req.end(); Chạy client.js hình Terminal khác để xem kết quả: http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/nodejs/index.jsp                                                                                                              Copyright  ©  vietjack.com     $ node client.js Kiểm tra kết quả: Vi du Web Module Node.js Hello World! Tiếp kiểm tra kết Server Server dang chay tai dia chi: http://127.0.0.1:8081/ Request cho /index.htm da duoc nhan http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   ... chi: http://127.0.0.1:8081/ Request cho /index.htm da duoc nhan Tạo Web Client sử dụng Node.js Để tạo Web Client, bạn sử dụng http Module Dưới ví dụ minh họa Tạo client.js có nội dung sau: var http... http://127.0.0.1:8081/'); Bây tạo index.htm thư mục với server.js: Vi du Web Module Node.js Hello World! Chạy server.js để xem kết quả:...  Copyright  ©  vietjack.com     • Data - Lớp bao gồm Database nguồn liệu Tạo Web Server sử dụng Node.js Node.js cung cấp http Module sử dụng để tạo HTTP client server Dưới phần kiến trúc thu nhỏ HTTP

Ngày đăng: 02/12/2017, 12:22

Xem thêm:

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN