這篇文章主要介紹了node.js中的http.response.writeHead方法使用說明,本文介紹了http.response.writeHead的方法說明、語法、接收參數、使用實例和實現源碼,需要的朋友可以參考下
方法說明:
向請求的客戶端發送響應頭。
該函數在一個請求內最多只能調用一次,如果不調用,則會自動生成一個響應頭。
語法:
復制代碼代碼如下:
response.writeHead(statusCode, [reasonPhrase], [headers])
接收參數:
statusCode HTTP狀態碼,如200(請求成功),404(未找到)等。
reasonPhrase
headers 類似關聯數組的對象,表示響應頭的每個屬性
例子:
復制代碼代碼如下:
var body = 'hello world';
response.writeHead(200, {
'Content-Length': body.length,
'Content-Type': 'text/plain'
});