本文基于Node.js v6.9.4,詳情請參考官網(wǎng)API
1. 創(chuàng)建服務(wù)器http.createServer
方式一和方式二效果一樣,方式一只不過是在創(chuàng)建服務(wù)器時,自動綁定了'request'事件
// 方式一 http.createServer([requestListener])
/*
* [requestListener(req, res)] 用戶請求后回調(diào)函數(shù),有兩個參數(shù),req請求;res響應(yīng)
* @return http.Server 返回一個http.Server實例
*/
var server = http.createServer((req, res) => {
res.writeHeader(200,{'Content-Type':'text/plain'});
res.end('hello world');
})
// 方式二 new http.Server()
var server = new http.Server();
server.on('request', (req, res){
res.writeHeader(200,{'Content-Type':'text/plain'});
res.end('hello world');
})
2. http.Server
上例中的server就是http.Server的一個實例
此模塊會觸發(fā)以下事件
- checkContinue 當(dāng)請求頭Expect的值是 100-continue時觸發(fā)
- checkExpectation 當(dāng)請求頭Expect的值不是 100-continue時觸發(fā)
- clientError 客戶端觸發(fā)了一個error錯誤
function(exception){}
- close 關(guān)閉服務(wù)器時觸發(fā)
function(errno){}
- connect 如果客戶端發(fā)起connect請求時觸發(fā)
// 當(dāng)一個新的TCP stream建立后發(fā)送此消息,stream是一個net.Stream的對象,通常用戶不會訪問/使用這個事件
function(stream){}
- connection 新的TCP流建立時觸發(fā)
- request 客戶端請求事件
/*
* req是http.ServerRequest的一個實例,
* res是http.ServerResponse的一個實例
*/
function(req, res){}
- upgrade 每當(dāng)一個客戶端請求一個http upgrade 時候發(fā)出此消息
/*
* socket是在服務(wù)器與客戶端之間連接用的網(wǎng)絡(luò)socket
* head 是Buffer 的一個實例
*/
function(req, socket, head){}
3. http.request(options[,callback])
客戶端向http服務(wù)器發(fā)起請求
/*
* options {hostname: string, port:number, method:string, path:string, handers:{}}
* hostname 服務(wù)器域名或IP地址
* port 端口
* method 請求方式,有GET、POST、INPUT、DELETE、CONNECT,默認(rèn)為GET
* path: 請求地址,可包含查詢字符串及可能村長的錨點,例如'/index.html?page=12'
* handers: 一個包含請求頭的對象
* @return http.ClientRequest 返回http.ClientRequest的實例
*/
var options = {
hostname : 'www.baidu.com',
port : 80,
method : 'GET',
path : '/upload',
handers:{
'Connection':'keep-alive', // 通知Node此鏈接保持到下一次請求
'Content-length': 1000, // 設(shè)置請求主體字節(jié)數(shù)
'Expect':'100-continue'
}
}
var req = http.request(options, (res) => {})
4. http.get(options,callback)
http.get是http.request的簡化版,唯一的區(qū)別在于http.get自動將請求方法設(shè)為了GET請求,同時不需要手動調(diào)用req.end()
var http = require('http');
http.createServer( (req, res) => {
}).listen(3000);
http.get('http://www.baidu.com/index.html', (res) => {
console.log('get response Code:'+ res.statusCode);
}).on('error', (e) => {
console.log('錯誤:'+ e.message);
})
5. http.ClientRequest
http.ClientRequest 是http.request或者h(yuǎn)ttp.get返回產(chǎn)生的對象,表示一個已經(jīng)產(chǎn)生而且正在進(jìn)行的http請求,提供一個response事件,也就是我們使用http.get和http.request方法中的回調(diào)函數(shù)所綁定的對象,我們可以顯示的綁定這個事件的監(jiān)聽函數(shù)。
var http = require('http');
var options = {
hostname: 'www.baidu.com',
port:'8080'
}
// 這里的req就是http.ClientRequest的一個實例,注意與http.IncomingMessage的區(qū)別
var req = http.request(options);
req.on('response', function(res){
res.setEncoding('utf-8');
res.on('data', function(chunk){
console.log(chunk.toString())
});
console.log(res.statusCode);
})
req.on('error', function(err){
console.log(err.message);
});
req.end();
http.ClientRequest也提供了write和end函數(shù),用于向服務(wù)器發(fā)送請求體,通常用于POST、PUT等操作,所有的寫操作都必須調(diào)用end函數(shù)來通知服務(wù)器,否則請求無效。
作為回調(diào)參數(shù)使用的對象
6. http.ServerResponse
http.ServerResponse是服務(wù)器返回給客戶端的信息,一般由http.Server的request事件發(fā)送,并作為第二個參數(shù)傳遞,它有三個重要的成員函數(shù),用戶返回響應(yīng)頭、響應(yīng)內(nèi)容已經(jīng)結(jié)束請求。
// 這里的res就是http.ServerResponse的一個實例
http.createServer( (req, res) => {})
6.1 res.writeHead(statusCode[,headers])
向用戶發(fā)送響應(yīng)頭,該函數(shù)在一個請求中最多調(diào)用一次,如果不調(diào)用,則自動生成一個響應(yīng)頭
6.2 res.write(data[,encoding])
向用戶發(fā)送響應(yīng)內(nèi)容,data是發(fā)送的內(nèi)容,encoding是編碼格式
6.3 res.end(data[,encoding])
結(jié)束響應(yīng),該函數(shù)必須被調(diào)用一次
7. http.IncomingMessage
http.IncomingMessage是HTTP請求的信息,是后端開發(fā)著最關(guān)注的內(nèi)容,一般由http.Serve的request事件發(fā)送,并作為第一個參數(shù)傳遞。
// 這里的req就是http.IncomingMessage的一個實例
var http = require('http')
http.createServer( (req, res) => {})