1, Module URL là gì?

-Module URL là một module được tích hợp sẵn vào trong core của node.js, nó được dùng để xử lý và phân tích chuỗi URL dựa vào đó chúng ta có thể biết được các thông số của URL đó.

2, Khai báo sử dụng Module URL.

-Như mình đã nói ở trên là: "module URL được tích hợp sẵn vào trong core của node.js" nên chúng ta không cần phải download hay cài đặt gì thêm cả.

-Để khai báo sử dụng module URL chúng ta sử dụng cú pháp:

javascript
copy
require('url');

-Module có các thuộc tính giống hệt như đối tượng location trong javascript nên mình sẽ không nhắc nghiều về lý thuyết nữa mà mình sẽ ví dụ luôn cho các bạn.

VD:

javascript
copy
var url = require('url'); var website = "http://toidicode.com?a=5"; //parse chuỗi sang url var parse = url.parse(website, true); //hiển thị host console.log('auth: ' + parse.auth); console.log('hash: ' + parse.hash); console.log('host: ' + parse.host); console.log('hostname: ' + parse.hostname); console.log('href: ' + parse.href); console.log('path: ' + parse.path); console.log('pathname: ' + parse.pathname); console.log('port: ' + parse.port); console.log('protocol: ' + parse.protocol); console.log('query: ' + parse.query.a); console.log('search: ' + parse.search); console.log('slashes: ' + parse.slashes);

Sau khi chạy đoạn code trên thì chúng ta thu được kết quả như sau:

http
copy
auth: null hash: null host: toidicode.com hostname: toidicode.com href: http://toidicode.com/?a=5 path: /?a=5 pathname: / port: null protocol: http: query: 5 search: ?a=5 slashes: true

3, Ví dụ.

-Ở phần này chúng ta sẽ cùng nhau xây dựng một web server có điều hướng url đơn giản kết hợp giữa 3 module đã được học là HTTP, fs và URL.

-Đầu tiên chúng ta sẽ tạo ra cấu trúc thư mục và file như sau:

sql
copy
| files |--| home.html |--| contact.html | server.js

File home.html sẽ có nội dung như sau:

html
copy
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Trang chủ</title> </head> <style type="text/css" media="screen"> h1{ text-align: center; } </style> <body> <h1>Đây là trang home</h1> </body> </html>

File contact.html

html
copy
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Contact</title> </head> <style type="text/css" media="screen"> h1{ text-align: center; } </style> <body> <h1>Đây là trang contact</h1> </body> </html>

-Sau khi đã có nội dung cho các file HTML bây giờ mình sẽ viết code để điều hướng các trang đó.

javascript
copy
var http = require('http'); var fs = require('fs'); var url = require('url'); http.createServer(function (request, response) { var parse = url.parse(request.url, true); var path = parse.path; //Nếu không truyền tham số lên url thì load trang home.html if (path === '/') { fs.readFile('files/home.html', function (error, data) { response.writeHead('200', {'Content-Type': 'text/html'}); response.end(data); }) } else { //ngược lại nếu truyền tham số lên url thì sẽ load trang tương ứng var load = "files" + path + '.html'; fs.readFile(load, function (error, data) { if (error) { response.writeHead('404', {'Content-Type': 'text/html'}); response.end('<h1>404 not found</h1>'); } else { response.writeHead('200', {'Content-Type': 'text/html'}); response.end(data); } }) } }).listen(8000);

-Sau khi chạy đoạn code trên thì các bạn sẽ thấy kế quả tương ứng 
localhost:8000/contact  => ra trang contact
localhost:8000              => ra trang home

4, Lời kết.

-Bài này chúng ta chỉ tìm hiểu cơ bản như thế thôi nhé, vì sau này chúng ta cũng không phải động đến nó nhiều đâu.