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

Đối tượng res biểu diễn HTTP Response mà ứng dụng Express gởi phản hồi khi nhận 1 HTTP Request.
Thuộc tính của Response trong Node.js
Bảng sau liệt kê những thuộc tính của đối tượng Response trong Node.js.
- res.app
Thuộc tính này giữ 1 tham chiếu đến ứng dụng Express mà đang dùng Middleware. - res.headersSent
Giá trị true, false để chỉ rằng ứng dụng có phải đã gởi HTTP Header cho Response hay là không. - res.locals
1 đối tượng chứa các biến cục bộ của Response trong khoảng Request.
Phương thức của Response trong Node.js
Phương thức res.append(subject [, value])
res.append(subject [, value])
Phương thức này phụ thêm 1 giá trị chi tiết đến trường HTTP Header của Response. Ví dụ:
res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);
res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');
res.append('Warning', 'Qua nhieu thong tin hon faucet');
Phương thức res.attachment([filename])
res.attachment([filename])
Phương thức này được dùng để gởi 1 File dưới dạng như là 1 Attachment trong HTTP Response. Ví dụ:
res.attachment('path/to/emblem.png');
Phương thức res.cookie(identify, worth [, options])
res.cookie(identify, worth [, options])
Phương thức này được dùng để thiết lập tên của Cookie thành worth. Tham số worth có thể là 1 chuỗi hay 1 đối tượng được chuyển đổi thành dạng JSON. Ví dụ:
res.cookie('identify', 'tobi', );
res.cookie('cart', objects: [1,2,3] );
res.cookie('cart', objects: [1,2,3] , {maxAge: 900000});
Phương thức res.clearCookie(identify [, options])
res.clearCookie(identify [, options])
Phương thức này được dùng để xóa các Cookie chi tiết được xác định do tham số identify. Ví dụ:
res.cookie('identify', 'tobi', { path: '/admin' });
res.clearCookie('identify', { path: '/admin' });
Phương thức res.obtain(path [, filename] [, fn])
res.obtain(path [, filename] [, fn])
Phương thức này được dùng để truyền File tại path đã cho dưới dạng 1 Attachment. Nhìn chung, trình duyệt sẽ nhắc người sử dụng để obtain. Ví dụ:
res.obtain('/report-12345.pdf');
res.obtain('/report-12345.pdf', 'report.pdf');
res.obtain('/report-12345.pdf', 'report.pdf', perform(err){ });
Phương thức res.finish([data] [, encoding])
res.finish([data] [, encoding])
Phương thức này được dùng để kết thúc tiến trình xử lý Response. Ví dụ:
res.finish();
res.standing(404).finish();
Phương thức res.format(object)
res.format(object)
Phương thức này được dùng để thực hiện định dạng phần Accept HTTP Header trên đối tượng Request khi biểu diễn. Ví dụ:
res.format({
'textual content/plain': perform(),
'textual content/html': perform(),
'software/json': perform(){
res.ship({ message: 'hey' });
},
'default': perform()
});
Phương thức res.get(subject)
res.get(subject)
Phương thức này được dùng để trả về 1 trường nào đấy trong HTTP Response Header. Ví dụ:
res.get('Content-Type');
Phương thức res.json([body])
res.json([body])
Phương thức này được dùng để gởi 1 phản hồi dưới định dạng JSON. Ví dụ:
res.json(null) res.json()
res.standing(500).json({ error: 'message' })
Phương thức res.jsonp([body])
res.jsonp([body])
Phương thức này được dùng để gởi 1 phản hồi dưới dạng định dạng JSON với sự hỗ trợ của JSONP. Ví dụ:
res.jsonp(null)
res.jsonp()
res.standing(500).jsonp({ error: 'message' })
Phương thức res.location(path)
res.location(path)
Phương thức này được dùng để thiết lập trường Location của HTTP Header dựa vào tham số path đã cho. Ví dụ:
res.location('/foo/bar');
res.location('foo/bar');
res.location('http://instance.com');
Phương thức res.redirect([status,] path)
res.redirect([status,] path)
Phương thức này được dùng để redirect đến 1 URL từ 1 đường truyền path chi tiết, với 1 Status Code nào đấy. Ví dụ:
res.redirect('/foo/bar');
res.redirect('http://instance.com');
res.redirect(301, 'http://instance.com');
Phương thức res.render(view [, locals] [, callback])
res.render(view [, locals] [, callback])
Phương thức này được dùng để render 1 view và gởi chuỗi HTML được render đến Client. Ví dụ:
// Gui mot view toi Client
res.render('index');
// Truyen mot bien cuc bo toi view
res.render('consumer', identify: 'Tobi' , perform(err, html) {
// ...
});
Phương thức res.ship([body])
res.ship([body])
Phương thức này được dùng để gởi HTTP Response. Ví dụ:
res.ship(new Buffer('whoop'));
res.ship({ some: 'json' });
res.ship('
some html');
Phương thức res.shipFile(path [, options] [, fn])
res.shipFile(path [, options] [, fn])
Phương thức này được dùng để truyền tải File tại địa chỉ path đã cho. Thiết lập Content-Type dựa vào phần đuôi của tên file. Ví dụ:
res.shipFile(fileName, choices, perform (err) {
// ...
});
Phương thức res.shipStatus(standingCode)
res.shipStatus(standingCode)
Phương thức này được dùng để thiết lập Status Code và gởi biểu diễn chuỗi của nó dưới dạng như là phần physique của Response. Ví dụ:
res.shipStatus(200); // tuong duong voi phuong thuc res.standing(200).ship('OK')
res.shipStatus(403); // tuong duong voi phuong thuc res.standing(403).ship('Forbidden')
res.shipStatus(404); // tuong duong voi phuong thuc res.standing(404).ship('Not Found')
res.shipStatus(500); // tuong duong voi phuong thuc res.standing(500).ship('Internal Server Error')
Phương thức res.set(subject [, value])
res.set(subject [, value])
Phương thức này được dùng để thiết lập trường subject của HTTP Header đến giá trị worth. Ví dụ:
res.set('Content-Type', 'textual content/plain');
res.set(
'Content-Type': 'textual content/plain',
'Content-Length': '123',
'ETag': '12345'
)
Phương thức res.standing(code)
res.standing(code)
Phương thức này được dùng để thiết lập HTTP Status cho Response. Ví dụ:
res.standing(403).finish();
res.standing(400).ship('Bad Request');
res.standing(404).shipFile('/absolute/path/to/404.png');
Phương thức res.sort(sort)
res.sort(sort)
Phương thức này được dùng để thiết lập Content-Type của HTTP Header thành kiểu MIME sort. Ví dụ:
res.sort('.html'); // => 'textual content/html'
res.sort('html'); // => 'textual content/html'
res.sort('json'); // => 'software/json'
res.sort('software/json'); // => 'software/json'
res.sort('png'); //