Mô-đun HTTP Node.js
Ví dụ
Tạo một máy chủ lắng nghe trên cổng 8080 trên máy tính của bạn.
Khi cổng 8080 được truy cập, hãy viết "Xin chào thế giới!" trở lại như một phản ứng:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello
World!');
res.end();
}).listen(8080);
Chạy ví dụ »Định nghĩa và cách sử dụng
Mô-đun HTTP cung cấp cách tạo dữ liệu truyền Node.js qua HTTP (Giao thức truyền siêu văn bản).
Cú pháp
Cú pháp để đưa mô-đun HTTP vào ứng dụng của bạn:
var http = require('http');
Thuộc tính và phương thức HTTP
Method | Description |
---|---|
createClient() | Deprecated. Creates a HTTP client |
createServer() | Creates an HTTP server |
get() | Sets the method to GET, and returns an object containing the user's request |
globalAgent | Returns the HTTP Agent |
request() | Returns an object containing the user's request |