Node.js HTTP Module
ตัวอย่าง
สร้างเซิร์ฟเวอร์ที่รับฟังพอร์ต 8080 ของคอมพิวเตอร์ของคุณ
เมื่อเข้าถึงพอร์ต 8080 ให้เขียนว่า "สวัสดีชาวโลก!" กลับมาเป็นคำตอบ:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello
World!');
res.end();
}).listen(8080);
ความหมายและการใช้งาน
โมดูล HTTP มีวิธีทำให้ Node.js ถ่ายโอนข้อมูลผ่าน HTTP (Hyper Text Transfer Protocol)
ไวยากรณ์
ไวยากรณ์สำหรับการรวมโมดูล HTTP ในแอปพลิเคชันของคุณ:
var http = require('http');
คุณสมบัติและวิธีการ 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 |