The HTTP request and response of node. js

Source: Internet
Author: User

In C #, OC is also the client initiates a request, the service side responds. We can make this process abstract and understandable.

1. Client-initiated requests to the server are equivalent to writing a stream to the server (writable)

2. Server-side read client stream (readable)

3. Service-side response to the client is equivalent to writing a stream to the client (writable)

4. Client reads the response from the server (readable)

The whole process is divided into two parts: the processing of the client and the service side. The most important is the client request and the server response.

First, HTTP. Clientrequest Object

It implements the writable stream and can use all of its functions. The Http.request (Options,callback) method is used in the HTTP module to generate the object.

The 1.options mainly includes the following options:

Host: Server domain name or IP address, default localhost

Hostname: Same as host, but support for Url.parse () is better than host.

Port: Ports

LocalAddress: Local Interface for network connections

Socketpath:unix domain sockets (using Host:port or Socketpath)

Method: Request methods: Get, post, etc. default get

Path: Requested resource path default/. Can also include query strings

Headers: request Header

Auth: Identity Authentication

Agent: Proxy. If the agent is used, the request defaults to connection:keep-alive. Possible values are Undefined,agent,false

Events and methods provided by 2.ClientRequest

Event:

Response: emitted when the response from the server receives the request. The callback handler receives a Incomingmessage object as a unique parameter.

Socket: Issued when a socket is assigned to the request.

Connect: Emitted when the server responds to a request initiated by the Connect method. If the event is not handled by the client, the connection is closed

Upgrade: Emitted when the server responds to a request that includes an update request on its header

Continue: When the server sends a 100ContinueHTTP response that instructs the client to send the request body

Method:

Write (), end () and writable stream

Abort (): Terminates the request.

SetTimeout (Timeout,[callback]): Set request time-out

Setnodelay ([Nodelay]): Nodelay Boolean, True writes immediately, false buffered write

Setsocketkeepalive ([Enable],[initualdelay]): Enables and disables the Keep-active feature for client requests. INITUALDELAY specifies the delay between the last packet and the first hold-active request.

Second, HTTP. Serverresponse Object

and HTTP. The Clientrequest object also implements the writable stream. You can also implement the functions of the writable stream, and also include the following event properties and methods.

Event or property:

Close: emitted when Response.End () is closed before the response is flushed.

Headerssent: Boolean value. Sent as true. Read-only.

Senddate: Boolean value, True when the date header is automatically generated and sent as part of the response

StatusCode: Specifies the response status code without displaying the write header.

Method:

Writecontinue (): Sends a http/1.1 continue message to the client, requesting the body content to be sent

Writehead (Statuscode,[reasonphrase],[headers]): Writes a response header to the request. The statuscode parameter is the HTTP response status code, Reasonphrase is a string that represents the reason for statuscode. Headers is the response header object. Response.writehead (, ' Success ', {' content-length ': body.length, ' content-type ': ' Text/plain '});

SetTimeout (Msecs,callback): Sets the time-out period for client connections.

SetHeader (Name,value): Sets the header value.

GetHeader (name): Gets the header value.

Removeheader (name): Removes the header.

Addtrailers (Headers): Writes the HTTP trailing header to the end of the response.

III. client response and server-side requests

The above one or two is primarily a writable stream, and the readable stream includes client response and server-side requests. They are all an object: the Incomingmessage object.

The Incomingmessage object grows into what it looks like. Because the Incomingmessage object is not used very often for our development, it is good to look at it.

var http=require ("http")var options={    hostname:' www.baidu.com ',    Path:',    port:' + ',    method:' GET '}; var  Req=http.request (options,function(response) {    Console.log (response);}); Req.end ();

The HTTP request and response of node. js

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.