module http
The http module encapsulates the processing of HTTP requests and responses, allowing us to easily create an http server or simulate a client to initiate an http request. Using the http module, developers can easily write and process code related to the HTTP protocol.
The following is a simple example to create a Web server and return a hello world response:
1
2
3
4
5
6
7const http = require('http');
const server = new http.Server(8080, function(request) {
request.response.write('Hello World!');
});
server.start();
In this example, we introduce the http module, then define an http server object and bind it to the local 8080 port number. When a request is sent to this port number, the response will be set to the string "Hello World!".
At the same time, the http module also contains client objects,http.ClientSimulate the browser environment to cache cookies and store them during the visiturlWhen carrying the corresponding cookie http client object. you can use ithttp.ClientAccess http interface requests, perform http downloads, and a series of http-related operations. Below ishttp.ClientApplication examples:
1
2
3
4var http = require('http');
var httpClient = new http.Client();
httpClient.get('http://fibjs.org');
In the above example, ahttp.Clientobject, and then calls get
the method to initiate an http GET request to fibjs.org.
in addition,http.ClientThere are some other properties and methods that can be called, such as cookies
etc.
The https module is an alias of the http module. You require('https')
can also use the http module.
object
Request
Create an http request object, seeHttpRequest
1HttpRequest http.Request;
Response
Create an http response object, seeHttpResponse
1HttpResponse http.Response;
Cookie
Create an http cookie object, seeHttpCookie
1HttpCookie http.Cookie;
Server
Create an http server, seeHttpServer
1HttpServer http.Server;
Client
Create an http client, seeHttpClient
1HttpClient http.Client;
HttpsServer
Create an https server, seeHttpsServer
1HttpsServer http.HttpsServer;
Handler
Create an http protocol handler object, seeHttpHandler
1HttpHandler http.Handler;
Repeater
Create an http request forwarding handler object, seeHttpRepeater
1HttpRepeater http.Repeater;
static function
fileHandler
Create an http static file processor to respond to http messages with static files
1
2
3static Handler http.fileHandler(String root,
Object mimes = {},
Boolean autoIndex = false);
Call parameters:
- root: String, file root path
- mimes: Object, extended mime settings
- autoIndex: Boolean, whether to support browsing directory files, the default is false, not supported
Return results:
- Handler, returns a static file processor for processing http messages
fileHandler supports gzip pre-compression. When the request accepts gzip encoding and the filename.ext.gz file exists in the same path, this file will be returned directly, thereby avoiding server load caused by repeated compression.
setClientCert
Set default client certificate
1
2static http.setClientCert(X509Cert crt,
PKey key);
Call parameters:
- crt:X509Cert, certificate, used to send to the server to verify the client
- key:PKey, private key, used to talk to the client
request
Send an http request to the specified stream object and return the result
1
2static HttpResponse http.request(Stream conn,
HttpRequest req) async;
Call parameters:
- conn:Stream, specifies the stream object to handle the request
- req:HttpRequest, to be sentHttpRequestobject
Return results:
- HttpResponse, returns the server response
Send an http request to the specified stream object and return the result
1
2
3static HttpResponse http.request(Stream conn,
HttpRequest req,
SeekableStream response_body) async;
Call parameters:
- conn:Stream, specifies the stream object to handle the request
- req:HttpRequest, to be sentHttpRequestobject
- response_body:SeekableStream, specify the stream of response.body
Return results:
- HttpResponse, returns the server response
request specifiedurl, and return the result
1
2
3static HttpResponse http.request(String method,
String url,
Object opts = {}) async;
Call parameters:
- method: String, specify http request method: GET, POST, etc.
- url: String, specifyurl, must be the complete containing hosturl
- opts: Object, specify additional information
Return results:
- HttpResponse, returns the server response
opts contains additional options for the request, supported as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16{
"method": "GET", // specify the http request method: GET, POST, etc, default: GET.
"protocol": "http",
"slashes": true,
"username": "",
"password": "",
"hostname": "",
"port": "",
"pathname": "",
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {},
"response_body": SeekableStream // specify response.body stream
}
Among them body,json, pack must not appear at the same time. The default is {}, which does not contain any additional information
Use the GET method to request the specifiedurl, and returns the result, which is equivalent to request("GET", ...)
1
2static HttpResponse http.request(String url,
Object opts = {}) async;
Call parameters:
- url: String, specifyurl, must be the complete containing hosturl
- opts: Object, specify additional information
Return results:
- HttpResponse, returns the server response
opts contains additional options for the request, supported as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15{
"method": "GET", // specify the http request method: GET, POST, etc, default: GET.
"protocol": "http",
"slashes": true,
"username": "",
"password": "",
"hostname": "",
"port": "",
"pathname": "",
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {}
}
Among them body,json, pack must not appear at the same time. The default is {}, which does not contain any additional information
Request options specifiedurl, and return the result
1static HttpResponse http.request(Object opts) async;
Call parameters:
- opts: Object, specify additional information
Return results:
- HttpResponse, returns the server response
opts contains additional options for the request, supported as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15{
"method": "GET", // specify the http request method: GET, POST, etc, default: GET.
"protocol": "http",
"slashes": true,
"username": "",
"password": "",
"hostname": "",
"port": "",
"pathname": "",
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {}
}
Among them body,json, pack must not appear at the same time. The default is {}, which does not contain any additional information
get
Use the GET method to request the specifiedurl, and returns the result, which is equivalent to request("GET", ...)
1
2static HttpResponse http.get(String url,
Object opts = {}) async;
Call parameters:
- url: String, specifyurl, must be the complete containing hosturl
- opts: Object, specify additional information
Return results:
- HttpResponse, returns the server response
opts contains additional options for the request, supported as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15{
"method": "GET", // specify the http request method: GET, POST, etc, default: GET.
"protocol": "http",
"slashes": true,
"username": "",
"password": "",
"hostname": "",
"port": "",
"pathname": "",
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {}
}
Among them body,json, pack must not appear at the same time. The default is {}, which does not contain any additional information
post
Use the POST method to request the specifiedurl, and returns the result, which is equivalent to request("POST", ...)
1
2static HttpResponse http.post(String url,
Object opts = {}) async;
Call parameters:
- url: String, specifyurl, must be the complete containing hosturl
- opts: Object, specify additional information
Return results:
- HttpResponse, returns the server response
opts contains additional options for the request, supported as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15{
"method": "GET", // specify the http request method: GET, POST, etc, default: GET.
"protocol": "http",
"slashes": true,
"username": "",
"password": "",
"hostname": "",
"port": "",
"pathname": "",
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {}
}
Among them body,json, pack must not appear at the same time. The default is {}, which does not contain any additional information
del
Use the DELETE method to request the specifiedurl, and returns the result, which is equivalent to request("DELETE", ...)
1
2static HttpResponse http.del(String url,
Object opts = {}) async;
Call parameters:
- url: String, specifyurl, must be the complete containing hosturl
- opts: Object, specify additional information
Return results:
- HttpResponse, returns the server response
opts contains additional options for the request, supported as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15{
"method": "GET", // specify the http request method: GET, POST, etc, default: GET.
"protocol": "http",
"slashes": true,
"username": "",
"password": "",
"hostname": "",
"port": "",
"pathname": "",
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {}
}
Among them body,json, pack must not appear at the same time. The default is {}, which does not contain any additional information
put
Use the PUT method to request the specifiedurl, and returns the result, which is equivalent to request("PUT", ...)
1
2static HttpResponse http.put(String url,
Object opts = {}) async;
Call parameters:
- url: String, specifyurl, must be the complete containing hosturl
- opts: Object, specify additional information
Return results:
- HttpResponse, returns the server response
opts contains additional options for the request, supported as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15{
"method": "GET", // specify the http request method: GET, POST, etc, default: GET.
"protocol": "http",
"slashes": true,
"username": "",
"password": "",
"hostname": "",
"port": "",
"pathname": "",
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {}
}
Among them body,json, pack must not appear at the same time. The default is {}, which does not contain any additional information
patch
Use the PATCH method to request the specifiedurl, and returns the result, which is equivalent to request("PATCH", ...)
1
2static HttpResponse http.patch(String url,
Object opts = {}) async;
Call parameters:
- url: String, specifyurl, must be the complete containing hosturl
- opts: Object, specify additional information
Return results:
- HttpResponse, returns the server response
opts contains additional options for the request, supported as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15{
"method": "GET", // specify the http request method: GET, POST, etc, default: GET.
"protocol": "http",
"slashes": true,
"username": "",
"password": "",
"hostname": "",
"port": "",
"pathname": "",
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {}
}
Among them body,json, pack must not appear at the same time. The default is {}, which does not contain any additional information
head
Use the HEAD method to request the specifiedurl, and returns the result, which is equivalent to request("HEAD", ...)
1
2static HttpResponse http.head(String url,
Object opts = {}) async;
Call parameters:
- url: String, specifyurl, must be the complete containing hosturl
- opts: Object, specify additional information
Return results:
- HttpResponse, returns the server response
opts contains additional options for the request, supported as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15{
"method": "GET", // specify the http request method: GET, POST, etc, default: GET.
"protocol": "http",
"slashes": true,
"username": "",
"password": "",
"hostname": "",
"port": "",
"pathname": "",
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {}
}
Among them body,json, pack must not appear at the same time. The default is {}, which does not contain any additional information
static properties
STATUS_CODES
Object, returns a collection of standard HTTP response status codes, and their respective short descriptions.
1static readonly Object http.STATUS_CODES;
cookies
NArray, returns the http clientHttpCookieobject list
1static readonly NArray http.cookies;
timeout
Integer, query and set timeout
1static Integer http.timeout;
enableCookie
Boolean, cookie function switch, enabled by default
1static Boolean http.enableCookie;
autoRedirect
Boolean, automatic redirect function switch, enabled by default
1static Boolean http.autoRedirect;
enableEncoding
Boolean, automatic decompression function switch, enabled by default
1static Boolean http.enableEncoding;
maxHeadersCount
Integer, query and set the maximum number of request headers, the default is 128
1static Integer http.maxHeadersCount;
maxHeaderSize
Integer, query and set the maximum request header length, the default is 8192
1static Integer http.maxHeaderSize;
maxBodySize
Integer, query and set the maximum size of the body, in MB, the default is -1, no size limit
1static Integer http.maxBodySize;
userAgent
String, query and set the browser identifier in http request
1static String http.userAgent;
poolSize
Integer, query and set the maximum number of keep-alive cache connections, default 128
1static Integer http.poolSize;
poolTimeout
Integer, query and set the keep-alive cache connection timeout, default 10000 ms
1static Integer http.poolTimeout;
http_proxy
String, query and set http request proxy, support http/https/socks5 proxy
1static String http.http_proxy;
https_proxy
String, query and set https request proxy, support http/https/socks5 proxy, if not set, or set to empty, http_proxy will be reused
1static String http.https_proxy;