Nodejs - Toidicode

Start from the beginning
                                        

-Để ghi nội dung mới vào một file thì chúng ta sử dụng phương thức writeFile() với cú pháp:

wireFile('filepath', 'content' , option, callback);

Trong đó:

filepath là đường dẫn của file cần ghi.content là nội dung mà chúng ta muốn ghi vào file.option có thể là chuỗi hoặc mảng chứa các tùy chọn encode hay mode của file, nếu không cần cấu hình thì mọi người có thể bỏ trống.callback là đoạn xử lý sau khi ghi file.

VD: Ghi một nội dung vào một file mới có tên writer.html.

var fs = require('fs');var content = 'Nội dung này tôi muốn ghi vào file writer.html';//sử dụng phương thức writeFile để ghi nội dung vào filefs.writeFile('writer.html',content,'utf8',function (err) { //Kiểm tra nếu có lỗi thì xuất ra lỗi if(err) throw err; else //nếu không thì hiển thị nội dung ghi file thành công console.log('Ghi file thanh cong!');});


Bài 11: Phân tích URL với module url trong nodejs1, 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:

require('url');

-Module có các thuộc tính giống hệt như đối tượng 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:

var url = require('url');var website = "http://toidicode.com?a=5";//parse chuỗi sang urlvar parse = url.parse(website,true);//hiển thị hostconsole.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:

auth: nullhash: nullhost: toidicode.comhostname: toidicode.comhref: http://toidicode.com/?a=5path: /?a=5pathname: /port: nullprotocol: http:query: 5search: ?a=5slashes: true3, 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:

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

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

<!DOCTYPE html><html><head> <meta> <meta http-equiv="X-UA-Compatible"> <title>Trang chủ</title></head><style> h1{ text-align: center; }</style><body> <h1>Đây là trang home</h1> </body></html>

You've reached the end of published parts.

⏰ Last updated: Mar 02, 2018 ⏰

Add this story to your Library to get notified about new parts!

Nodejs - ToidicodeWhere stories live. Discover now