Nodejs - Toidicode

Start from the beginning
                                        

-Sau khi chúng ta đã tạo xong view rồi, thì việc tiếp tục chúng ta cần làm là dùng module fs để render file view đó ra trên server.

Code:

var http = require('http'); var fs = require('fs'); http.createServer(function (req, res) { //xét header cho request res.writeHead('200',{'Content-Type': 'text/html'}); //Đọc file index và trả về dữ liệu fs.readFile('index.html','utf8',function (err,data) { //nếu nỗi thì thông báo if (err) throw err; //không lỗi thì render data res.end(data); }) }).listen(8000);

Lúc này chúng ta chạy server lên thì kết quả sẽ được như hình.

-Bây giờ trên file server.js chúng ta sẽ tiếp tục require module formidable vào để viết code xử lý upload.

var http = require('http'); var fs = require('fs'); var formidable = require('formidable'); http.createServer(function (req, res) { //Nếu request là uplooad và method là post if (req.url == '/upload' && req.method.toLowerCase() == 'post') { //Khởi tạo form var form = new formidable.IncomingForm(); //Thiết lập thư mục chứa file trên server form.uploadDir = "uploads/"; //xử lý upload form.parse(req,function (err, fields, file) { //path tmp trên server var path = file.files.path; //thiết lập path mới cho file var newpath = form.uploadDir + file.files.name; fs.rename(path, newpath, function (err) { if (err) throw err; res.end('Upload Thanh cong!'); }); }); return ; } //xét header cho request res.writeHead('200',{'Content-Type': 'text/html'}); //Đọc file index và trả về dữ liệu fs.readFile('index.html','utf8',function (err,data) { //nếu nỗi thì thông báo if (err) throw err; //không lỗi thì render data res.end(data); }) }).listen(8000);

-Lúc này các bạn chạy lại server và hưởng thụ kết quả!


Bài 14: Event trong node.js1, Event Loop.

-Node.js sử dụng rất nhiều sự kiện, nên đó cũng là 1 trong các yếu tố khiến cho node.js thực thi nhanh hơn các ngôn ngữ khác. Khi node.js bắt đầu chạy thì nó hoàn toàn chưa có gì xảy ra, cho đến khi nó bắt được các sự kiện thì nó mới thực thi code cho từng sự kiện và các sự kiện đó có thể sẽ lặp đi lặp lại trong suốt quá trình thực thi ứng dụng.

Thêm sự kiện.

-Để sử dụng được event trong Node.js thì chúng ta cần phải require module event và khởi tạo đối tượng eventEimitter.

Cú Pháp:

//Require module eventvar event = require('events');//Khởi tạo đối tượng eventEmittervar eventEmitter = new event.EventEmitter();

-Bây giờ khi muốn thêm một sự kiện mới thì chúng ta sẽ sử dụng phương thức on theo cú pháp sau:

on(eventName, handleEvent);

Trong đó:

eventName - Là tên của sự kiện bạn muốn thêm.handleEvent - là hàm xử lý khi eventName được gọi. handleEvent có thể là một callbackfunction hoặc là một hàm ẩn danh (closure function).

VD1: Thêm sự kiện connection với handle là closure function.

var event = require('events'); var eventEmitter = new event.EventEmitter(); eventEmitter.on('connection', function () { console.log('connection successful'); });

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