- var http = require('http'); //通過 require方法把 HTTP庫包含到程序中來
- http.createServer(function (req, res) { //創建新的 HTTP服務器。新創建的 HTTP服務器並沒有賦值給任何變量,它只會成爲存活在全局範圍內的匿名對象
- res.writeHead(200, {'Content-Type': 'text/plain'}); //響應頭:頭描述text/plain,狀態碼:200
- res.end('Hello World\n'); //返回字符串
- }).listen(8124, "127.0.0.1"); //監聽ip地址和端口
- console.log('Server running at http://127.0.0.1:8124/'); //log
end方法將會關閉 HTTP連接。但因爲我們同時還傳入了一個字符串, end方法將在把此內容發送給客戶端後才關閉連接。
源鏈接:http://book.51cto.com/art/201301/377179.htm