1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width"> 6 <meta name="nodejs.org:node-version" content="v14.20.1"> 7 <title>HTTPS | Node.js v14.20.1 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/https.html"> 12</head> 13<body class="alt apidoc" id="api-section-https"> 14 <div id="content" class="clearfix"> 15 <div id="column2" class="interior"> 16 <div id="intro" class="interior"> 17 <a href="/" title="Go back to the home page"> 18 Node.js 19 </a> 20 </div> 21 <ul> 22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 24</ul> 25<hr class="line"> 26<ul> 27<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 29<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 30<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 31<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 33<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 35<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 36<li><a href="console.html" class="nav-console">Console</a></li> 37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 38<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 42<li><a href="dns.html" class="nav-dns">DNS</a></li> 43<li><a href="domain.html" class="nav-domain">Domain</a></li> 44<li><a href="errors.html" class="nav-errors">Errors</a></li> 45<li><a href="events.html" class="nav-events">Events</a></li> 46<li><a href="fs.html" class="nav-fs">File system</a></li> 47<li><a href="globals.html" class="nav-globals">Globals</a></li> 48<li><a href="http.html" class="nav-http">HTTP</a></li> 49<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 50<li><a href="https.html" class="nav-https active">HTTPS</a></li> 51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 52<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li> 56<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 57<li><a href="net.html" class="nav-net">Net</a></li> 58<li><a href="os.html" class="nav-os">OS</a></li> 59<li><a href="path.html" class="nav-path">Path</a></li> 60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 61<li><a href="policy.html" class="nav-policy">Policies</a></li> 62<li><a href="process.html" class="nav-process">Process</a></li> 63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 65<li><a href="readline.html" class="nav-readline">Readline</a></li> 66<li><a href="repl.html" class="nav-repl">REPL</a></li> 67<li><a href="report.html" class="nav-report">Report</a></li> 68<li><a href="stream.html" class="nav-stream">Stream</a></li> 69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 70<li><a href="timers.html" class="nav-timers">Timers</a></li> 71<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 73<li><a href="tty.html" class="nav-tty">TTY</a></li> 74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 75<li><a href="url.html" class="nav-url">URL</a></li> 76<li><a href="util.html" class="nav-util">Utilities</a></li> 77<li><a href="v8.html" class="nav-v8">V8</a></li> 78<li><a href="vm.html" class="nav-vm">VM</a></li> 79<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 82</ul> 83<hr class="line"> 84<ul> 85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 86</ul> 87 </div> 88 89 <div id="column1" data-id="https" class="interior"> 90 <header> 91 <div class="header-container"> 92 <h1>Node.js v14.20.1 documentation</h1> 93 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 94 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 95 <path fill="none" d="M0 0h24v24H0z" /> 96 <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> 97 <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> 98 </svg> 99 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 100 <path d="M0 0h24v24H0z" fill="none" /> 101 <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> 102 </svg> 103 </button> 104 </div> 105 <div id="gtoc"> 106 <ul> 107 <li> 108 <a href="index.html">Index</a> 109 </li> 110 <li> 111 <a href="all.html">View on single page</a> 112 </li> 113 <li> 114 <a href="https.json">View as JSON</a> 115 </li> 116 117 <li class="version-picker"> 118 <a href="#">View another version <span>▼</span></a> 119 <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v18.x/api/https.html">18.x</a></li> 120<li><a href="https://nodejs.org/docs/latest-v17.x/api/https.html">17.x</a></li> 121<li><a href="https://nodejs.org/docs/latest-v16.x/api/https.html">16.x <b>LTS</b></a></li> 122<li><a href="https://nodejs.org/docs/latest-v15.x/api/https.html">15.x</a></li> 123<li><a href="https://nodejs.org/docs/latest-v14.x/api/https.html">14.x <b>LTS</b></a></li> 124<li><a href="https://nodejs.org/docs/latest-v13.x/api/https.html">13.x</a></li> 125<li><a href="https://nodejs.org/docs/latest-v12.x/api/https.html">12.x <b>LTS</b></a></li> 126<li><a href="https://nodejs.org/docs/latest-v11.x/api/https.html">11.x</a></li> 127<li><a href="https://nodejs.org/docs/latest-v10.x/api/https.html">10.x</a></li> 128<li><a href="https://nodejs.org/docs/latest-v9.x/api/https.html">9.x</a></li> 129<li><a href="https://nodejs.org/docs/latest-v8.x/api/https.html">8.x</a></li> 130<li><a href="https://nodejs.org/docs/latest-v7.x/api/https.html">7.x</a></li> 131<li><a href="https://nodejs.org/docs/latest-v6.x/api/https.html">6.x</a></li> 132<li><a href="https://nodejs.org/docs/latest-v5.x/api/https.html">5.x</a></li> 133<li><a href="https://nodejs.org/docs/latest-v4.x/api/https.html">4.x</a></li> 134<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/https.html">0.12.x</a></li> 135<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/https.html">0.10.x</a></li></ol> 136 </li> 137 138 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/https.md">Edit on GitHub</a></li> 139 </ul> 140 </div> 141 <hr> 142 </header> 143 144 <details id="toc" open><summary>Table of contents</summary><ul> 145<li><span class="stability_2"><a href="#https_https">HTTPS</a></span> 146<ul> 147<li><a href="#https_class_https_agent">Class: <code>https.Agent</code></a> 148<ul> 149<li><a href="#https_new_agent_options"><code>new Agent([options])</code></a> 150<ul> 151<li><a href="#https_event_keylog">Event: <code>'keylog'</code></a></li> 152</ul> 153</li> 154</ul> 155</li> 156<li><a href="#https_class_https_server">Class: <code>https.Server</code></a> 157<ul> 158<li><a href="#https_server_close_callback"><code>server.close([callback])</code></a></li> 159<li><a href="#https_server_headerstimeout"><code>server.headersTimeout</code></a></li> 160<li><a href="#https_server_listen"><code>server.listen()</code></a></li> 161<li><a href="#https_server_maxheaderscount"><code>server.maxHeadersCount</code></a></li> 162<li><a href="#https_server_requesttimeout"><code>server.requestTimeout</code></a></li> 163<li><a href="#https_server_settimeout_msecs_callback"><code>server.setTimeout([msecs][, callback])</code></a></li> 164<li><a href="#https_server_timeout"><code>server.timeout</code></a></li> 165<li><a href="#https_server_keepalivetimeout"><code>server.keepAliveTimeout</code></a></li> 166</ul> 167</li> 168<li><a href="#https_https_createserver_options_requestlistener"><code>https.createServer([options][, requestListener])</code></a></li> 169<li><a href="#https_https_get_options_callback"><code>https.get(options[, callback])</code></a></li> 170<li><a href="#https_https_get_url_options_callback"><code>https.get(url[, options][, callback])</code></a></li> 171<li><a href="#https_https_globalagent"><code>https.globalAgent</code></a></li> 172<li><a href="#https_https_request_options_callback"><code>https.request(options[, callback])</code></a></li> 173<li><a href="#https_https_request_url_options_callback"><code>https.request(url[, options][, callback])</code></a></li> 174</ul> 175</li> 176</ul></details> 177 178 <div id="apicontent"> 179 <h2>HTTPS<span><a class="mark" href="#https_https" id="https_https">#</a></span></h2> 180 181<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p> 182<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.20.1/lib/https.js">lib/https.js</a></p> 183<p>HTTPS is the HTTP protocol over TLS/SSL. In Node.js this is implemented as a 184separate module.</p> 185<section><h3>Class: <code>https.Agent</code><span><a class="mark" href="#https_class_https_agent" id="https_class_https_agent">#</a></span></h3> 186<div class="api_metadata"> 187<details class="changelog"><summary>History</summary> 188<table> 189<tbody><tr><th>Version</th><th>Changes</th></tr> 190<tr><td>v5.3.0</td> 191<td><p>support <code>0</code> <code>maxCachedSessions</code> to disable TLS session caching.</p></td></tr> 192<tr><td>v2.5.0</td> 193<td><p>parameter <code>maxCachedSessions</code> added to <code>options</code> for TLS sessions reuse.</p></td></tr> 194<tr><td>v0.4.5</td> 195<td><p><span>Added in: v0.4.5</span></p></td></tr> 196</tbody></table> 197</details> 198</div> 199<p>An <a href="#https_class_https_agent"><code>Agent</code></a> object for HTTPS similar to <a href="http.html#http_class_http_agent"><code>http.Agent</code></a>. See 200<a href="#https_https_request_options_callback"><code>https.request()</code></a> for more information.</p> 201<h4><code>new Agent([options])</code><span><a class="mark" href="#https_new_agent_options" id="https_new_agent_options">#</a></span></h4> 202<div class="api_metadata"> 203<details class="changelog"><summary>History</summary> 204<table> 205<tbody><tr><th>Version</th><th>Changes</th></tr> 206<tr><td>v12.5.0</td> 207<td><p>do not automatically set servername if the target host was specified using an IP address.</p></td></tr> 208</tbody></table> 209</details> 210</div> 211<ul> 212<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Set of configurable options to set on the agent. 213Can have the same fields as for <a href="http.html#http_new_agent_options"><code>http.Agent(options)</code></a>, and 214<ul> 215<li> 216<p><code>maxCachedSessions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> maximum number of TLS cached sessions. 217Use <code>0</code> to disable TLS session caching. <strong>Default:</strong> <code>100</code>.</p> 218</li> 219<li> 220<p><code>servername</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> the value of 221<a href="https://en.wikipedia.org/wiki/Server_Name_Indication">Server Name Indication extension</a> to be sent to the server. Use 222empty string <code>''</code> to disable sending the extension. 223<strong>Default:</strong> host name of the target server, unless the target server 224is specified using an IP address, in which case the default is <code>''</code> (no 225extension).</p> 226<p>See <a href="tls.html#tls_session_resumption"><code>Session Resumption</code></a> for information about TLS session reuse.</p> 227</li> 228</ul> 229</li> 230</ul> 231<h5>Event: <code>'keylog'</code><span><a class="mark" href="#https_event_keylog" id="https_event_keylog">#</a></span></h5> 232<div class="api_metadata"> 233<span>Added in: v13.2.0, v12.16.0</span> 234</div> 235<ul> 236<li><code>line</code> <a href="buffer.html#buffer_class_buffer" class="type"><Buffer></a> Line of ASCII text, in NSS <code>SSLKEYLOGFILE</code> format.</li> 237<li><code>tlsSocket</code> <a href="tls.html#tls_class_tls_tlssocket" class="type"><tls.TLSSocket></a> The <code>tls.TLSSocket</code> instance on which it was 238generated.</li> 239</ul> 240<p>The <code>keylog</code> event is emitted when key material is generated or received by a 241connection managed by this agent (typically before handshake has completed, but 242not necessarily). This keying material can be stored for debugging, as it 243allows captured TLS traffic to be decrypted. It may be emitted multiple times 244for each socket.</p> 245<p>A typical use case is to append received lines to a common text file, which is 246later used by software (such as Wireshark) to decrypt the traffic:</p> 247<pre><code class="language-js"><span class="hljs-comment">// ...</span> 248https.<span class="hljs-property">globalAgent</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'keylog'</span>, <span class="hljs-function">(<span class="hljs-params">line, tlsSocket</span>) =></span> { 249 fs.<span class="hljs-title function_">appendFileSync</span>(<span class="hljs-string">'/tmp/ssl-keys.log'</span>, line, { <span class="hljs-attr">mode</span>: <span class="hljs-number">0o600</span> }); 250});</code></pre> 251</section><section><h3>Class: <code>https.Server</code><span><a class="mark" href="#https_class_https_server" id="https_class_https_server">#</a></span></h3> 252<div class="api_metadata"> 253<span>Added in: v0.3.4</span> 254</div> 255<ul> 256<li>Extends: <a href="tls.html#tls_class_tls_server" class="type"><tls.Server></a></li> 257</ul> 258<p>See <a href="http.html#http_class_http_server"><code>http.Server</code></a> for more information.</p> 259<h4><code>server.close([callback])</code><span><a class="mark" href="#https_server_close_callback" id="https_server_close_callback">#</a></span></h4> 260<div class="api_metadata"> 261<span>Added in: v0.1.90</span> 262</div> 263<ul> 264<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 265<li>Returns: <a href="https.html#https_class_https_server" class="type"><https.Server></a></li> 266</ul> 267<p>See <a href="http.html#http_server_close_callback"><code>server.close()</code></a> from the HTTP module for details.</p> 268<h4><code>server.headersTimeout</code><span><a class="mark" href="#https_server_headerstimeout" id="https_server_headerstimeout">#</a></span></h4> 269<div class="api_metadata"> 270<span>Added in: v11.3.0</span> 271</div> 272<ul> 273<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>60000</code></li> 274</ul> 275<p>See <a href="http.html#http_server_headerstimeout"><code>http.Server#headersTimeout</code></a>.</p> 276<h4><code>server.listen()</code><span><a class="mark" href="#https_server_listen" id="https_server_listen">#</a></span></h4> 277<p>Starts the HTTPS server listening for encrypted connections. 278This method is identical to <a href="net.html#net_server_listen"><code>server.listen()</code></a> from <a href="net.html#net_class_net_server"><code>net.Server</code></a>.</p> 279<h4><code>server.maxHeadersCount</code><span><a class="mark" href="#https_server_maxheaderscount" id="https_server_maxheaderscount">#</a></span></h4> 280<ul> 281<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>2000</code></li> 282</ul> 283<p>See <a href="http.html#http_server_maxheaderscount"><code>http.Server#maxHeadersCount</code></a>.</p> 284<h4><code>server.requestTimeout</code><span><a class="mark" href="#https_server_requesttimeout" id="https_server_requesttimeout">#</a></span></h4> 285<div class="api_metadata"> 286<span>Added in: v14.11.0</span> 287</div> 288<ul> 289<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>0</code></li> 290</ul> 291<p>See <a href="http.html#http_server_requesttimeout"><code>http.Server#requestTimeout</code></a>.</p> 292<h4><code>server.setTimeout([msecs][, callback])</code><span><a class="mark" href="#https_server_settimeout_msecs_callback" id="https_server_settimeout_msecs_callback">#</a></span></h4> 293<div class="api_metadata"> 294<span>Added in: v0.11.2</span> 295</div> 296<ul> 297<li><code>msecs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>120000</code> (2 minutes)</li> 298<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 299<li>Returns: <a href="https.html#https_class_https_server" class="type"><https.Server></a></li> 300</ul> 301<p>See <a href="http.html#http_server_settimeout_msecs_callback"><code>http.Server#setTimeout()</code></a>.</p> 302<h4><code>server.timeout</code><span><a class="mark" href="#https_server_timeout" id="https_server_timeout">#</a></span></h4> 303<div class="api_metadata"> 304<details class="changelog"><summary>History</summary> 305<table> 306<tbody><tr><th>Version</th><th>Changes</th></tr> 307<tr><td>v13.0.0</td> 308<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr> 309<tr><td>v0.11.2</td> 310<td><p><span>Added in: v0.11.2</span></p></td></tr> 311</tbody></table> 312</details> 313</div> 314<ul> 315<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> 0 (no timeout)</li> 316</ul> 317<p>See <a href="http.html#http_server_timeout"><code>http.Server#timeout</code></a>.</p> 318<h4><code>server.keepAliveTimeout</code><span><a class="mark" href="#https_server_keepalivetimeout" id="https_server_keepalivetimeout">#</a></span></h4> 319<div class="api_metadata"> 320<span>Added in: v8.0.0</span> 321</div> 322<ul> 323<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>5000</code> (5 seconds)</li> 324</ul> 325<p>See <a href="http.html#http_server_keepalivetimeout"><code>http.Server#keepAliveTimeout</code></a>.</p> 326</section><section><h3><code>https.createServer([options][, requestListener])</code><span><a class="mark" href="#https_https_createserver_options_requestlistener" id="https_https_createserver_options_requestlistener">#</a></span></h3> 327<div class="api_metadata"> 328<span>Added in: v0.3.4</span> 329</div> 330<ul> 331<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Accepts <code>options</code> from <a href="tls.html#tls_tls_createserver_options_secureconnectionlistener"><code>tls.createServer()</code></a>, 332<a href="tls.html#tls_tls_createsecurecontext_options"><code>tls.createSecureContext()</code></a> and <a href="http.html#http_http_createserver_options_requestlistener"><code>http.createServer()</code></a>.</li> 333<li><code>requestListener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A listener to be added to the <code>'request'</code> event.</li> 334<li>Returns: <a href="https.html#https_class_https_server" class="type"><https.Server></a></li> 335</ul> 336<pre><code class="language-js"><span class="hljs-comment">// curl -k https://localhost:8000/</span> 337<span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span class="hljs-string">'https'</span>); 338<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>); 339 340<span class="hljs-keyword">const</span> options = { 341 <span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'test/fixtures/keys/agent2-key.pem'</span>), 342 <span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'test/fixtures/keys/agent2-cert.pem'</span>) 343}; 344 345https.<span class="hljs-title function_">createServer</span>(options, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { 346 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>); 347 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>); 348}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code></pre> 349<p>Or</p> 350<pre><code class="language-js"><span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span class="hljs-string">'https'</span>); 351<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>); 352 353<span class="hljs-keyword">const</span> options = { 354 <span class="hljs-attr">pfx</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'test/fixtures/test_cert.pfx'</span>), 355 <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'sample'</span> 356}; 357 358https.<span class="hljs-title function_">createServer</span>(options, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { 359 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>); 360 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>); 361}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code></pre> 362</section><section><h3><code>https.get(options[, callback])</code><span><a class="mark" href="#https_https_get_options_callback" id="https_https_get_options_callback">#</a></span></h3> 363</section><section><h3><code>https.get(url[, options][, callback])</code><span><a class="mark" href="#https_https_get_url_options_callback" id="https_https_get_url_options_callback">#</a></span></h3> 364<div class="api_metadata"> 365<details class="changelog"><summary>History</summary> 366<table> 367<tbody><tr><th>Version</th><th>Changes</th></tr> 368<tr><td>v10.9.0</td> 369<td><p>The <code>url</code> parameter can now be passed along with a separate <code>options</code> object.</p></td></tr> 370<tr><td>v7.5.0</td> 371<td><p>The <code>options</code> parameter can be a WHATWG <code>URL</code> object.</p></td></tr> 372<tr><td>v0.3.6</td> 373<td><p><span>Added in: v0.3.6</span></p></td></tr> 374</tbody></table> 375</details> 376</div> 377<ul> 378<li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> 379<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> Accepts the same <code>options</code> as 380<a href="#https_https_request_options_callback"><code>https.request()</code></a>, with the <code>method</code> always set to <code>GET</code>.</li> 381<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 382</ul> 383<p>Like <a href="http.html#http_http_get_options_callback"><code>http.get()</code></a> but for HTTPS.</p> 384<p><code>options</code> can be an object, a string, or a <a href="url.html#url_the_whatwg_url_api"><code>URL</code></a> object. If <code>options</code> is a 385string, it is automatically parsed with <a href="url.html#url_new_url_input_base"><code>new URL()</code></a>. If it is a <a href="url.html#url_the_whatwg_url_api"><code>URL</code></a> 386object, it will be automatically converted to an ordinary <code>options</code> object.</p> 387<pre><code class="language-js"><span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span class="hljs-string">'https'</span>); 388 389https.<span class="hljs-title function_">get</span>(<span class="hljs-string">'https://encrypted.google.com/'</span>, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> { 390 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'statusCode:'</span>, res.<span class="hljs-property">statusCode</span>); 391 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'headers:'</span>, res.<span class="hljs-property">headers</span>); 392 393 res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">d</span>) =></span> { 394 process.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">write</span>(d); 395 }); 396 397}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 398 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(e); 399});</code></pre> 400</section><section><h3><code>https.globalAgent</code><span><a class="mark" href="#https_https_globalagent" id="https_https_globalagent">#</a></span></h3> 401<div class="api_metadata"> 402<span>Added in: v0.5.9</span> 403</div> 404<p>Global instance of <a href="#https_class_https_agent"><code>https.Agent</code></a> for all HTTPS client requests.</p> 405</section><section><h3><code>https.request(options[, callback])</code><span><a class="mark" href="#https_https_request_options_callback" id="https_https_request_options_callback">#</a></span></h3> 406</section><section><h3><code>https.request(url[, options][, callback])</code><span><a class="mark" href="#https_https_request_url_options_callback" id="https_https_request_url_options_callback">#</a></span></h3> 407<div class="api_metadata"> 408<details class="changelog"><summary>History</summary> 409<table> 410<tbody><tr><th>Version</th><th>Changes</th></tr> 411<tr><td>v14.18.0</td> 412<td><p>When using a <code>URL</code> object parsed username and password will now be properly URI decoded.</p></td></tr> 413<tr><td>v14.1.0</td> 414<td><p>The <code>highWaterMark</code> option is accepted now.</p></td></tr> 415<tr><td>v10.9.0</td> 416<td><p>The <code>url</code> parameter can now be passed along with a separate <code>options</code> object.</p></td></tr> 417<tr><td>v9.3.0</td> 418<td><p>The <code>options</code> parameter can now include <code>clientCertEngine</code>.</p></td></tr> 419<tr><td>v7.5.0</td> 420<td><p>The <code>options</code> parameter can be a WHATWG <code>URL</code> object.</p></td></tr> 421<tr><td>v0.3.6</td> 422<td><p><span>Added in: v0.3.6</span></p></td></tr> 423</tbody></table> 424</details> 425</div> 426<ul> 427<li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a></li> 428<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#url_the_whatwg_url_api" class="type"><URL></a> Accepts all <code>options</code> from 429<a href="http.html#http_http_request_options_callback"><code>http.request()</code></a>, with some differences in default values: 430<ul> 431<li><code>protocol</code> <strong>Default:</strong> <code>'https:'</code></li> 432<li><code>port</code> <strong>Default:</strong> <code>443</code></li> 433<li><code>agent</code> <strong>Default:</strong> <code>https.globalAgent</code></li> 434</ul> 435</li> 436<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 437<li>Returns: <a href="http.html#http_class_http_clientrequest" class="type"><http.ClientRequest></a></li> 438</ul> 439<p>Makes a request to a secure web server.</p> 440<p>The following additional <code>options</code> from <a href="tls.html#tls_tls_connect_options_callback"><code>tls.connect()</code></a> are also accepted: 441<code>ca</code>, <code>cert</code>, <code>ciphers</code>, <code>clientCertEngine</code>, <code>crl</code>, <code>dhparam</code>, <code>ecdhCurve</code>, 442<code>honorCipherOrder</code>, <code>key</code>, <code>passphrase</code>, <code>pfx</code>, <code>rejectUnauthorized</code>, 443<code>secureOptions</code>, <code>secureProtocol</code>, <code>servername</code>, <code>sessionIdContext</code>, 444<code>highWaterMark</code>.</p> 445<p><code>options</code> can be an object, a string, or a <a href="url.html#url_the_whatwg_url_api"><code>URL</code></a> object. If <code>options</code> is a 446string, it is automatically parsed with <a href="url.html#url_new_url_input_base"><code>new URL()</code></a>. If it is a <a href="url.html#url_the_whatwg_url_api"><code>URL</code></a> 447object, it will be automatically converted to an ordinary <code>options</code> object.</p> 448<p><code>https.request()</code> returns an instance of the <a href="http.html#http_class_http_clientrequest"><code>http.ClientRequest</code></a> 449class. The <code>ClientRequest</code> instance is a writable stream. If one needs to 450upload a file with a POST request, then write to the <code>ClientRequest</code> object.</p> 451<pre><code class="language-js"><span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span class="hljs-string">'https'</span>); 452 453<span class="hljs-keyword">const</span> options = { 454 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'encrypted.google.com'</span>, 455 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>, 456 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>, 457 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span> 458}; 459 460<span class="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> { 461 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'statusCode:'</span>, res.<span class="hljs-property">statusCode</span>); 462 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'headers:'</span>, res.<span class="hljs-property">headers</span>); 463 464 res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">d</span>) =></span> { 465 process.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">write</span>(d); 466 }); 467}); 468 469req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 470 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(e); 471}); 472req.<span class="hljs-title function_">end</span>();</code></pre> 473<p>Example using options from <a href="tls.html#tls_tls_connect_options_callback"><code>tls.connect()</code></a>:</p> 474<pre><code class="language-js"><span class="hljs-keyword">const</span> options = { 475 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'encrypted.google.com'</span>, 476 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>, 477 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>, 478 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>, 479 <span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'test/fixtures/keys/agent2-key.pem'</span>), 480 <span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'test/fixtures/keys/agent2-cert.pem'</span>) 481}; 482options.<span class="hljs-property">agent</span> = <span class="hljs-keyword">new</span> https.<span class="hljs-title class_">Agent</span>(options); 483 484<span class="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> { 485 <span class="hljs-comment">// ...</span> 486});</code></pre> 487<p>Alternatively, opt out of connection pooling by not using an <a href="#https_class_https_agent"><code>Agent</code></a>.</p> 488<pre><code class="language-js"><span class="hljs-keyword">const</span> options = { 489 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'encrypted.google.com'</span>, 490 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>, 491 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>, 492 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>, 493 <span class="hljs-attr">key</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'test/fixtures/keys/agent2-key.pem'</span>), 494 <span class="hljs-attr">cert</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'test/fixtures/keys/agent2-cert.pem'</span>), 495 <span class="hljs-attr">agent</span>: <span class="hljs-literal">false</span> 496}; 497 498<span class="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> { 499 <span class="hljs-comment">// ...</span> 500});</code></pre> 501<p>Example using a <a href="url.html#url_the_whatwg_url_api"><code>URL</code></a> as <code>options</code>:</p> 502<pre><code class="language-js"><span class="hljs-keyword">const</span> options = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'https://abc:xyz@example.com'</span>); 503 504<span class="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> { 505 <span class="hljs-comment">// ...</span> 506});</code></pre> 507<p>Example pinning on certificate fingerprint, or the public key (similar to 508<code>pin-sha256</code>):</p> 509<pre><code class="language-js"><span class="hljs-keyword">const</span> tls = <span class="hljs-built_in">require</span>(<span class="hljs-string">'tls'</span>); 510<span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span class="hljs-string">'https'</span>); 511<span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>); 512 513<span class="hljs-keyword">function</span> <span class="hljs-title function_">sha256</span>(<span class="hljs-params">s</span>) { 514 <span class="hljs-keyword">return</span> crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>).<span class="hljs-title function_">update</span>(s).<span class="hljs-title function_">digest</span>(<span class="hljs-string">'base64'</span>); 515} 516<span class="hljs-keyword">const</span> options = { 517 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'github.com'</span>, 518 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>, 519 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>, 520 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>, 521 <span class="hljs-attr">checkServerIdentity</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">host, cert</span>) { 522 <span class="hljs-comment">// Make sure the certificate is issued to the host we are connected to</span> 523 <span class="hljs-keyword">const</span> err = tls.<span class="hljs-title function_">checkServerIdentity</span>(host, cert); 524 <span class="hljs-keyword">if</span> (err) { 525 <span class="hljs-keyword">return</span> err; 526 } 527 528 <span class="hljs-comment">// Pin the public key, similar to HPKP pin-sha25 pinning</span> 529 <span class="hljs-keyword">const</span> pubkey256 = <span class="hljs-string">'pL1+qb9HTMRZJmuC/bB/ZI9d302BYrrqiVuRyW+DGrU='</span>; 530 <span class="hljs-keyword">if</span> (<span class="hljs-title function_">sha256</span>(cert.<span class="hljs-property">pubkey</span>) !== pubkey256) { 531 <span class="hljs-keyword">const</span> msg = <span class="hljs-string">'Certificate verification error: '</span> + 532 <span class="hljs-string">`The public key of '<span class="hljs-subst">${cert.subject.CN}</span>' `</span> + 533 <span class="hljs-string">'does not match our pinned fingerprint'</span>; 534 <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(msg); 535 } 536 537 <span class="hljs-comment">// Pin the exact certificate, rather than the pub key</span> 538 <span class="hljs-keyword">const</span> cert256 = <span class="hljs-string">'25:FE:39:32:D9:63:8C:8A:FC:A1:9A:29:87:'</span> + 539 <span class="hljs-string">'D8:3E:4C:1D:98:DB:71:E4:1A:48:03:98:EA:22:6A:BD:8B:93:16'</span>; 540 <span class="hljs-keyword">if</span> (cert.<span class="hljs-property">fingerprint256</span> !== cert256) { 541 <span class="hljs-keyword">const</span> msg = <span class="hljs-string">'Certificate verification error: '</span> + 542 <span class="hljs-string">`The certificate of '<span class="hljs-subst">${cert.subject.CN}</span>' `</span> + 543 <span class="hljs-string">'does not match our pinned fingerprint'</span>; 544 <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(msg); 545 } 546 547 <span class="hljs-comment">// This loop is informational only.</span> 548 <span class="hljs-comment">// Print the certificate and public key fingerprints of all certs in the</span> 549 <span class="hljs-comment">// chain. Its common to pin the public key of the issuer on the public</span> 550 <span class="hljs-comment">// internet, while pinning the public key of the service in sensitive</span> 551 <span class="hljs-comment">// environments.</span> 552 <span class="hljs-keyword">do</span> { 553 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Subject Common Name:'</span>, cert.<span class="hljs-property">subject</span>.<span class="hljs-property">CN</span>); 554 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">' Certificate SHA256 fingerprint:'</span>, cert.<span class="hljs-property">fingerprint256</span>); 555 556 hash = crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 557 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">' Public key ping-sha256:'</span>, <span class="hljs-title function_">sha256</span>(cert.<span class="hljs-property">pubkey</span>)); 558 559 lastprint256 = cert.<span class="hljs-property">fingerprint256</span>; 560 cert = cert.<span class="hljs-property">issuerCertificate</span>; 561 } <span class="hljs-keyword">while</span> (cert.<span class="hljs-property">fingerprint256</span> !== lastprint256); 562 563 }, 564}; 565 566options.<span class="hljs-property">agent</span> = <span class="hljs-keyword">new</span> https.<span class="hljs-title class_">Agent</span>(options); 567<span class="hljs-keyword">const</span> req = https.<span class="hljs-title function_">request</span>(options, <span class="hljs-function">(<span class="hljs-params">res</span>) =></span> { 568 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'All OK. Server matched our pinned cert or public key'</span>); 569 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'statusCode:'</span>, res.<span class="hljs-property">statusCode</span>); 570 <span class="hljs-comment">// Print the HPKP values</span> 571 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'headers:'</span>, res.<span class="hljs-property">headers</span>[<span class="hljs-string">'public-key-pins'</span>]); 572 573 res.<span class="hljs-title function_">on</span>(<span class="hljs-string">'data'</span>, <span class="hljs-function">(<span class="hljs-params">d</span>) =></span> {}); 574}); 575 576req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 577 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(e.<span class="hljs-property">message</span>); 578}); 579req.<span class="hljs-title function_">end</span>();</code></pre> 580<p>Outputs for example:</p> 581<pre><code class="language-text">Subject Common Name: github.com 582 Certificate SHA256 fingerprint: 25:FE:39:32:D9:63:8C:8A:FC:A1:9A:29:87:D8:3E:4C:1D:98:DB:71:E4:1A:48:03:98:EA:22:6A:BD:8B:93:16 583 Public key ping-sha256: pL1+qb9HTMRZJmuC/bB/ZI9d302BYrrqiVuRyW+DGrU= 584Subject Common Name: DigiCert SHA2 Extended Validation Server CA 585 Certificate SHA256 fingerprint: 40:3E:06:2A:26:53:05:91:13:28:5B:AF:80:A0:D4:AE:42:2C:84:8C:9F:78:FA:D0:1F:C9:4B:C5:B8:7F:EF:1A 586 Public key ping-sha256: RRM1dGqnDFsCJXBTHky16vi1obOlCgFFn/yOhI/y+ho= 587Subject Common Name: DigiCert High Assurance EV Root CA 588 Certificate SHA256 fingerprint: 74:31:E5:F4:C3:C1:CE:46:90:77:4F:0B:61:E0:54:40:88:3B:A9:A0:1E:D0:0B:A6:AB:D7:80:6E:D3:B1:18:CF 589 Public key ping-sha256: WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18= 590All OK. Server matched our pinned cert or public key 591statusCode: 200 592headers: max-age=0; pin-sha256="WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18="; pin-sha256="RRM1dGqnDFsCJXBTHky16vi1obOlCgFFn/yOhI/y+ho="; pin-sha256="k2v657xBsOVe1PQRwOsHsw3bsGT2VzIqz5K+59sNQws="; pin-sha256="K87oWBWM9UZfyddvDfoxL+8lpNyoUB2ptGtn0fv6G2Q="; pin-sha256="IQBnNBEiFuhj+8x6X8XLgh01V9Ic5/V3IRQLNFFc7v4="; pin-sha256="iie1VXtL7HzAMF+/PVPR9xzT80kQxdZeJ+zduCB3uj0="; pin-sha256="LvRiGEjRqfzurezaWuj8Wie2gyHMrW5Q06LspMnox7A="; includeSubDomains</code></pre></section> 593 <!-- API END --> 594 </div> 595 </div> 596 </div> 597 <script> 598 'use strict'; 599 { 600 const kCustomPreference = 'customDarkTheme'; 601 const userSettings = sessionStorage.getItem(kCustomPreference); 602 const themeToggleButton = document.getElementById('theme-toggle-btn'); 603 if (userSettings === null && window.matchMedia) { 604 const mq = window.matchMedia('(prefers-color-scheme: dark)'); 605 if ('onchange' in mq) { 606 function mqChangeListener(e) { 607 document.body.classList.toggle('dark-mode', e.matches); 608 } 609 mq.addEventListener('change', mqChangeListener); 610 if (themeToggleButton) { 611 themeToggleButton.addEventListener('click', function() { 612 mq.removeEventListener('change', mqChangeListener); 613 }, { once: true }); 614 } 615 } 616 if (mq.matches) { 617 document.body.classList.add('dark-mode'); 618 } 619 } else if (userSettings === 'true') { 620 document.body.classList.add('dark-mode'); 621 } 622 if (themeToggleButton) { 623 themeToggleButton.hidden = false; 624 themeToggleButton.addEventListener('click', function() { 625 sessionStorage.setItem( 626 kCustomPreference, 627 document.body.classList.toggle('dark-mode') 628 ); 629 }); 630 } 631 } 632 </script> 633</body> 634</html> 635