{ "source": "doc/api/https.md", "modules": [ { "textRaw": "HTTPS", "name": "https", "stability": 2, "stabilityText": "Stable", "desc": "
HTTPS is the HTTP protocol over TLS/SSL. In Node.js this is implemented as a\nseparate module.
\n", "classes": [ { "textRaw": "Class: https.Agent", "type": "class", "name": "https.Agent", "meta": { "added": [ "v0.4.5" ] }, "desc": "An Agent object for HTTPS similar to http.Agent
. See https.request()
\nfor more information.
This class is a subclass of tls.Server
and emits events same as\nhttp.Server
. See http.Server
for more information.
See http.Server#timeout
.
Returns a new HTTPS web server object. The options
is similar to\ntls.createServer()
. The requestListener
is a function which is\nautomatically added to the 'request'
event.
Example:
\n// curl -k https://localhost:8000/\nconst https = require('https');\nconst fs = require('fs');\n\nconst options = {\n key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),\n cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')\n};\n\nhttps.createServer(options, (req, res) => {\n res.writeHead(200);\n res.end('hello world\\n');\n}).listen(8000);\n
\nOr
\nconst https = require('https');\nconst fs = require('fs');\n\nconst options = {\n pfx: fs.readFileSync('test/fixtures/test_cert.pfx'),\n passphrase: 'sample'\n};\n\nhttps.createServer(options, (req, res) => {\n res.writeHead(200);\n res.end('hello world\\n');\n}).listen(8000);\n
\n",
"methods": [
{
"textRaw": "server.close([callback])",
"type": "method",
"name": "close",
"meta": {
"added": [
"v0.1.90"
]
},
"desc": "See http.close()
for details.
See http.listen()
for details.
See http.listen()
for details.
Like http.get()
but for HTTPS.
options
can be an object or a string. If options
is a string, it is\nautomatically parsed with url.parse()
.
Example:
\nconst https = require('https');\n\nhttps.get('https://encrypted.google.com/', (res) => {\n console.log('statusCode:', res.statusCode);\n console.log('headers:', res.headers);\n\n res.on('data', (d) => {\n process.stdout.write(d);\n });\n\n}).on('error', (e) => {\n console.error(e);\n});\n
\n",
"signatures": [
{
"params": [
{
"name": "options"
},
{
"name": "callback"
}
]
}
]
},
{
"textRaw": "https.request(options, callback)",
"type": "method",
"name": "request",
"meta": {
"added": [
"v0.3.6"
]
},
"desc": "Makes a request to a secure web server.
\noptions
can be an object or a string. If options
is a string, it is\nautomatically parsed with url.parse()
.
All options from http.request()
are valid.
Example:
\nconst https = require('https');\n\nconst options = {\n hostname: 'encrypted.google.com',\n port: 443,\n path: '/',\n method: 'GET'\n};\n\nconst req = https.request(options, (res) => {\n console.log('statusCode:', res.statusCode);\n console.log('headers:', res.headers);\n\n res.on('data', (d) => {\n process.stdout.write(d);\n });\n});\n\nreq.on('error', (e) => {\n console.error(e);\n});\nreq.end();\n
\nThe options argument has the following options
\nhost
: A domain name or IP address of the server to issue the request to.\nDefaults to 'localhost'
.hostname
: Alias for host
. To support url.parse()
hostname
is\npreferred over host
.family
: IP address family to use when resolving host
and hostname
.\nValid values are 4
or 6
. When unspecified, both IP v4 and v6 will be\nused.port
: Port of remote server. Defaults to 443.localAddress
: Local interface to bind for network connections.socketPath
: Unix Domain Socket (use one of host:port or socketPath).method
: A string specifying the HTTP request method. Defaults to 'GET'
.path
: Request path. Defaults to '/'
. Should include query string if any.\nE.G. '/index.html?page=12'
. An exception is thrown when the request path\ncontains illegal characters. Currently, only spaces are rejected but that\nmay change in the future.headers
: An object containing request headers.auth
: Basic authentication i.e. 'user:password'
to compute an\nAuthorization header.agent
: Controls Agent
behavior. When an Agent is used request will\ndefault to Connection: keep-alive
. Possible values:undefined
(default): use globalAgent
for this host and port.Agent
object: explicitly use the passed in Agent
.false
: opts out of connection pooling with an Agent, defaults request to\nConnection: close
.The following options from tls.connect()
can also be specified:
pfx
: Certificate, Private key and CA certificates to use for SSL. Default null
.key
: Private key to use for SSL. Default null
.passphrase
: A string of passphrase for the private key or pfx. Default null
.cert
: Public x509 certificate to use. Default null
.ca
: A string, Buffer
or array of strings or Buffer
s of trusted\ncertificates in PEM format. If this is omitted several well known "root"\nCAs will be used, like VeriSign. These are used to authorize connections.ciphers
: A string describing the ciphers to use or exclude. Consult\nhttps://www.openssl.org/docs/man1.0.2/apps/ciphers.html#CIPHER-LIST-FORMAT for\ndetails on the format.rejectUnauthorized
: If true
, the server certificate is verified against\nthe list of supplied CAs. An 'error'
event is emitted if verification\nfails. Verification happens at the connection level, before the HTTP\nrequest is sent. Default true
.secureProtocol
: The SSL method to use, e.g. SSLv3_method
to force\nSSL version 3. The possible values depend on your installation of\nOpenSSL and are defined in the constant SSL_METHODS
.servername
: Servername for SNI (Server Name Indication) TLS extension.In order to specify these options, use a custom Agent
.
Example:
\nconst options = {\n hostname: 'encrypted.google.com',\n port: 443,\n path: '/',\n method: 'GET',\n key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),\n cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')\n};\noptions.agent = new https.Agent(options);\n\nconst req = https.request(options, (res) => {\n // ...\n});\n
\nAlternatively, opt out of connection pooling by not using an Agent
.
Example:
\nconst options = {\n hostname: 'encrypted.google.com',\n port: 443,\n path: '/',\n method: 'GET',\n key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),\n cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem'),\n agent: false\n};\n\nconst req = https.request(options, (res) => {\n // ...\n});\n
\n",
"signatures": [
{
"params": [
{
"name": "options"
},
{
"name": "callback"
}
]
}
]
}
],
"properties": [
{
"textRaw": "https.globalAgent",
"name": "globalAgent",
"meta": {
"added": [
"v0.5.9"
]
},
"desc": "Global instance of https.Agent
for all HTTPS client requests.