node. JS (11)--request/get Implementation Comment function

Source: Internet
Author: User

HTTP Get/request can initiate an HTTP request from the background to obtain remote resources

, and even updating and synchronizing remote resources is very useful.

Essentially, the request returns an instance of the Clientrequest class.

This clientrequest instance is a writable stream, if you need to upload a file using a POST request,

Then this file will be written to the Clientrequest object,

How to use it?

Syntax: Http.request (Options[,callback])


If the first parameter is an object, a series of configurations can be made to customize the format of the request we need to make.

Common parameters are:

Host:java Server domain name or IP address

Aliases for Hostname:host

Port: Port number default is 80

LocalAddress: A local interface for binding to a network connection

Method: A String that specifies the methods of the HTTP request. The default is ' GET '

Path: The route of the request. The default is '/'. The query string, if any, should be included. Like '/index.html?page=12 '

Headers: An object containing the request header

AUTH: The Basic authentication used to calculate the authentication head, usually the user to keep up with password

Agent: Controlling the behavior of the agent

KeepAlive: Keep the socket around the resource in the future can be continued for other requests, default is False


To implement the submit function of a comment ...

Key references: Request Headers

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/8F/2F/wKioL1jXFSnRafW5AABIqkfAhDA321.jpg "title=" 36020170315204858576.jpg "alt=" Wkiol1jxfsnrafw5aabiqkfahda321.jpg "/>
The second is: Response, which is usually the JSON data returned

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

We need to copy the contents of the request headers.

These parameters for the background server to do the analysis to determine that you are logged in and is a real user, then this review is legal,

Var http = require (' http ')//Can serialize an object Var querystring = require (' QueryString ') Var postdata = querystring.stringify ({' content ': ' Preparing for the second quarter ', ' 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 ',//modify the length here to change ' content-length ':p ostdata.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= Qzn2u2ogvhm2i0nge0mgnmnmnjymezn2q5yta0m2uaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Aaaaamjkwmdcxngaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4ndu3nzkynjvacXeuy29taaaaaaaaaaaaaaaaaaaaaguwzmflmdjiyjg4ytbkmjcxmme5yjcyyzjlzjixnzhhb0hwwg9b1lg%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))// We send it in a stream when we receive the data, so it//triggers a On.data event res.on (' Data ', function (chunk) {Console.log (Buffer.isbuffer (chunk)) Console.log (Typeof chunk)})//Network End Res.on (' End ', function () {console.log (' comment complete ')})}) reQ.on (' Error ', function (e) {console.log (' ERROR: ' +e.message)})//writereq.write (postdata)//endreq.end () 

The results of the operation are as follows:

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/8F/31/wKiom1jXGn-TeVquAACPYTNoDy4074.jpg "title=" 36020170315204858576.jpg "alt=" Wkiom1jxgn-tevquaacpytnody4074.jpg "/> as follows (to manually refresh the page because the comment is made locally and does not make an AJAX request):

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 method, most of the cases we just submit a request to query, in fact there is no specific

Data submission, this time will use the Get method, and the request method difference is:

Method is set to get and automatically calls end methods


This article is from the "It Rookie" blog, make sure to keep this source http://mazongfei.blog.51cto.com/3174958/1910420

node. JS (11)--request/get Implementation Comment function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.