Node.js(十一)——request/get實現評論功能

來源:互聯網
上載者:User

標籤:node.js(十一)——request/get實現評論功能

http的get/request可以從後台發起一個http請求從而可以擷取遠端資源

,甚至是更新和同步遠端資源,非常有用。

本質上request返回的是一個clientRequest類的執行個體

這個clientRequest的執行個體是一個可寫的流,如果你需要使用post請求上傳一個檔案,

那麼這個檔案就會被寫入到clientRequest對象裡面,

該怎麼用的?

文法:http.request(options[,callback])


如果第一個參數是對象的話,就可以進行一系列的配置,來定製我們需要發出的請求的格式,

常見參數有:

host:java伺服器網域名稱或者是IP地址

hostName:host的別名

port:連接埠號碼 預設是80

localAddress:用於綁定到網路連接的本地介面

method:一個指定 HTTP 要求方法的字串。預設為 ‘GET‘

path:請求的路徑。預設為 ‘/‘。 應包括查詢字串(如有的話)。如 ‘/index.html?page=12‘

headers :一個包含要求標頭的對象

auth:用於計算認證頭的基本認證,一般是user跟上password

agent :控制 Agent 的行為

keepAlive:保持資源周圍的通訊端在未來能被繼續用於其他請求,預設是false


來實現一個評論的提交功能。。。

重點參考:Request Headers

650) this.width=650;" src="https://s4.51cto.com/wyfs02/M01/8F/2F/wKioL1jXFSnRafW5AABIqkfAhDA321.jpg" title="36020170315204858576.jpg" alt="wKioL1jXFSnRafW5AABIqkfAhDA321.jpg" />
其次是:Response,返回的一般是json資料

650) this.width=650;" src="https://s1.51cto.com/wyfs02/M02/8F/31/wKiom1jXFk3CC1oUAAAUyr2Rs4s048.jpg" title="36020170315204858576.jpg" alt="wKiom1jXFk3CC1oUAAAUyr2Rs4s048.jpg" />

我們需要把Request Headers裡的內容拷出來

這一些參數對於後台伺服器做解析判斷你是登入的並且是真實使用者,那這個評論是合法的,

var http = require(‘http‘)//可以把一個對象序列化var querystring = require(‘querystring‘)var postData = querystring.stringify({‘content‘:‘正在準備第二季‘,‘cid‘:348})var options = {hostname:‘www.imooc.com‘,port:80,path:‘/course/docomment‘,method:‘POST‘,headers:{‘Accept‘:‘application/json, text/javascript, */*; q=0.01‘,‘Accept-Encoding‘:‘gzip, deflate‘,‘Accept-Language‘:‘zh-CN,zh;q=0.8‘,‘Connection‘:‘keep-alive‘,//這裡的長度修改更改‘Content-Length‘:postData.length,‘Content-Type‘:‘application/x-www-form-urlencoded; charset=UTF-8‘,‘Cookie‘:‘imooc_uuid=80a28286-15ef-4fe7-a10f-341b99d11357; imooc_isnew_ct=1486736221; bdshare_firstime=1486736247756; AJSTAT_ok_times=18; PHPSESSID=mjhe6ik85emsonjhsn7v3t9mp4; loginstate=1; apsid=QzN2U2OGVhM2I0NGE0MGNmNmNjYmEzN2Q5YTA0M2UAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjkwMDcxNgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA4NDU3NzkyNjVAcXEuY29tAAAAAAAAAAAAAAAAAAAAAGUwZmFlMDJiYjg4YTBkMjcxMmE5YjcyYzJlZjIxNzhhb0HWWG9B1lg%3DOW; last_login_username=845779265%40qq.com; Hm_lvt_f0cfcccd7b1393990c78efdeebff3968=1490406351,1490435767,1490435811,1490450848; Hm_lpvt_f0cfcccd7b1393990c78efdeebff3968=1490490428; IMCDNS=0; imooc_isnew=2; cvde=58d5080dadc94-140‘,‘Host‘:‘www.imooc.com‘,‘Origin‘:‘http://www.imooc.com‘,‘Referer‘:‘http://www.imooc.com/comment/348‘,‘User-Agent‘:‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36‘,‘X-Requested-With‘:‘XMLHttpRequest‘}}var req = http.request(options,function(res){console.log(‘Status:‘+res.statusCode)console.log(‘headers:‘+JSON.stringify(res.headers))//我們在接收資料的時候是已流的方式發送上來的,所以會//觸發一個on.data事件res.on(‘data‘,function(chunk){console.log(Buffer.isBuffer(chunk))console.log(typeof chunk)})//網路結束res.on(‘end‘,function(){console.log(‘評論完畢‘)})})req.on(‘error‘,function(e){console.log(‘Error:‘+e.message)})//writereq.write(postData)//endreq.end()

運行結果如下:

650) this.width=650;" src="https://s1.51cto.com/wyfs02/M00/8F/31/wKiom1jXGn-TeVquAACPYTNoDy4074.jpg" title="36020170315204858576.jpg" alt="wKiom1jXGn-TeVquAACPYTNoDy4074.jpg" />如下(要手動重新整理頁面,因為評論是在本地進行的,不會進行ajax請求):

650) this.width=650;" src="https://s1.51cto.com/wyfs02/M01/8F/31/wKiom1jXGrfgr_AqAAAYa2Wz_bo072.jpg" title="36020170315204858576.jpg" alt="wKiom1jXGrfgr_AqAAAYa2Wz_bo072.jpg" />

get方法,大多數情況下我們只是提交一個請求用來查詢,其實並沒有具體的

資料提交,這個時候就會用get方法;和request方法區別是:

method設定為get同時會自動調用end方法


本文出自 “IT菜鳥” 部落格,請務必保留此出處http://mazongfei.blog.51cto.com/3174958/1910420

Node.js(十一)——request/get實現評論功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.