Mô-đun HTTP Node.js
Thí dụ
Tạo một máy chủ lắng nghe trên cổng 8080 của máy tính của bạn.
Khi cổng 8080 được truy cập, hãy viết "Hello World!" trở lại như một phản hồi:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello
World!');
res.end();
}).listen(8080);
Định nghĩa và Cách sử dụng
Mô-đun HTTP cung cấp một cách để Node.js truyền dữ liệu qua HTTP (Giao thức truyền siêu văn bản).
Cú pháp
Cú pháp để bao gồm mô-đun HTTP trong ứ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 |