¥
createConnection
<Function>
A function that produces a socket/stream to
use for the request when the
agent
option is not used. This can be used to
avoid creating a custom
Agent
class just to override the default
createConnection
function. See
agent.createConnection()
for more
details. Any
Duplex
stream is a valid return value.
¥
defaultPort
<number>
Default port for the protocol.
Default:
agent.defaultPort
if an
Agent
is used, else
undefined
.
family
<number>
解析
host
或
hostname
时使用的 IP 地址族。有效值为
4
或
6
。当未指定时,则将使用 IP v4 和 v6。
¥
family
<number>
IP address family to use when resolving
host
or
hostname
. Valid values are
4
or
6
. When unspecified, both IP v4 and
v6 will be used.
¥
insecureHTTPParser
<boolean>
If set to
true
, it will use a HTTP parser
with leniency flags enabled. Using the insecure parser should be avoided.
See
--insecure-http-parser
for more information.
Default:
false
¥
joinDuplicateHeaders
<boolean>
It joins the field line values of
multiple headers in a request with
,
instead of discarding
the duplicates. See
message.headers
for more information.
Default:
false
.
¥
maxHeaderSize
<number>
Optionally overrides the value of
--max-http-header-size
(the maximum length of response headers in
bytes) for responses received from the server.
Default:
16384 (16 KiB).
¥
path
<string>
Request path. Should include query string if any.
E.G.
'/index.html?page=12'
. An exception is thrown when the request path
contains illegal characters. Currently, only spaces are rejected but that
may change in the future.
Default:
'/'
.
port
<number>
远程服务器的端口。默认值:如果设置则为
defaultPort
,否则为
80
。
¥
port
<number>
Port of remote server.
Default:
defaultPort
if set,
else
80
.
¥
setDefaultHeaders
<boolean>
: Specifies whether or not to automatically add
default headers such as
Connection
,
Content-Length
,
Transfer-Encoding
,
and
Host
. If set to
false
then all necessary headers must be added
manually. Defaults to
true
.
¥
setHost
<boolean>
: Specifies whether or not to automatically add the
Host
header. If provided, this overrides
setDefaultHeaders
. Defaults to
true
.
¥
url
can be a string or a
URL
object. If
url
is a
string, it is automatically parsed with
new URL()
. If it is a
URL
object, it will be automatically converted to an ordinary
options
object.
如果同时指定了
url
和
options
,则合并对象,
options
属性优先。
¥If both
url
and
options
are specified, the objects are merged, with the
options
properties taking precedence.
¥The optional
callback
parameter will be added as a one-time listener for
the
'response'
event.
http.request()
返回
http.ClientRequest
类的实例。
ClientRequest
实例是可写流。如果需要使用 POST 请求上传文件,则写入
ClientRequest
对象。
¥
http.request()
returns an instance of the
http.ClientRequest
class. The
ClientRequest
instance is a writable stream. If one needs to
upload a file with a POST request, then write to the
ClientRequest
object.
¥In the example
req.end()
was called. With
http.request()
one
must always call
req.end()
to signify the end of the request -
even if there is no data being written to the request body.
如果在请求期间遇到任何错误(无论是 DNS 解析、TCP 级别错误还是实际的 HTTP 解析错误),都会在返回的请求对象上触发
'error'
事件。与所有
'error'
事件一样,如果没有注册监听器,则会抛出错误。
¥If any error is encountered during the request (be that with DNS resolution,
TCP level errors, or actual HTTP parse errors) an
'error'
event is emitted
on the returned request object. As with all
'error'
events, if no listeners
are registered the error will be thrown.
有一些特殊的标头需要注意。
¥There are a few special headers that should be noted.
发送“连接:keep-alive' 将通知 Node.js 与服务器的连接应该保持到下一个请求。
¥Sending a 'Connection: keep-alive' will notify Node.js that the connection to
the server should be persisted until the next request.
发送 '内容长度' 标头将禁用默认的分块编码。
¥Sending a 'Content-Length' header will disable the default chunked encoding.
¥Sending an 'Expect' header will immediately send the request headers.
Usually, when sending 'Expect: 100-continue', both a timeout and a listener
for the
'continue'
event should be set. See RFC 2616 Section 8.2.3 for more
information.
发送授权标头将覆盖使用
auth
选项来计算基本身份验证。
¥Sending an Authorization header will override using the
auth
option
to compute basic authentication.
¥Setting the
timeout
option or using the
setTimeout()
function will
not abort the request or do anything besides add a
'timeout'
event.
传递
AbortSignal
,然后在相应的
AbortController
上调用
abort()
的行为方式与在请求中调用
.destroy()
的方式相同。具体来说,
'error'
事件将触发错误消息
'AbortError: The operation was aborted'
、代码
'ABORT_ERR'
和
cause
(如果提供的话)。
¥Passing an
AbortSignal
and then calling
abort()
on the corresponding
AbortController
will behave the same way as calling
.destroy()
on the
request. Specifically, the
'error'
event will be emitted with an error with
the message
'AbortError: The operation was aborted'
, the code
'ABORT_ERR'
and the
cause
, if one was provided.