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.19.1"> 7 <title>HTTPS | Node.js v14.19.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.19.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-v17.x/api/https.html">17.x</a></li> 120<li><a href="https://nodejs.org/docs/latest-v16.x/api/https.html">16.x <b>LTS</b></a></li> 121<li><a href="https://nodejs.org/docs/latest-v15.x/api/https.html">15.x</a></li> 122<li><a href="https://nodejs.org/docs/latest-v14.x/api/https.html">14.x <b>LTS</b></a></li> 123<li><a href="https://nodejs.org/docs/latest-v13.x/api/https.html">13.x</a></li> 124<li><a href="https://nodejs.org/docs/latest-v12.x/api/https.html">12.x <b>LTS</b></a></li> 125<li><a href="https://nodejs.org/docs/latest-v11.x/api/https.html">11.x</a></li> 126<li><a href="https://nodejs.org/docs/latest-v10.x/api/https.html">10.x</a></li> 127<li><a href="https://nodejs.org/docs/latest-v9.x/api/https.html">9.x</a></li> 128<li><a href="https://nodejs.org/docs/latest-v8.x/api/https.html">8.x</a></li> 129<li><a href="https://nodejs.org/docs/latest-v7.x/api/https.html">7.x</a></li> 130<li><a href="https://nodejs.org/docs/latest-v6.x/api/https.html">6.x</a></li> 131<li><a href="https://nodejs.org/docs/latest-v5.x/api/https.html">5.x</a></li> 132<li><a href="https://nodejs.org/docs/latest-v4.x/api/https.html">4.x</a></li> 133<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/https.html">0.12.x</a></li> 134<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/https.html">0.10.x</a></li></ol> 135 </li> 136 137 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/https.md">Edit on GitHub</a></li> 138 </ul> 139 </div> 140 <hr> 141 </header> 142 143 <details id="toc" open><summary>Table of contents</summary><ul> 144<li><span class="stability_2"><a href="#https_https">HTTPS</a></span> 145<ul> 146<li><a href="#https_class_https_agent">Class: <code>https.Agent</code></a> 147<ul> 148<li><a href="#https_new_agent_options"><code>new Agent([options])</code></a> 149<ul> 150<li><a href="#https_event_keylog">Event: <code>'keylog'</code></a></li> 151</ul> 152</li> 153</ul> 154</li> 155<li><a href="#https_class_https_server">Class: <code>https.Server</code></a> 156<ul> 157<li><a href="#https_server_close_callback"><code>server.close([callback])</code></a></li> 158<li><a href="#https_server_headerstimeout"><code>server.headersTimeout</code></a></li> 159<li><a href="#https_server_listen"><code>server.listen()</code></a></li> 160<li><a href="#https_server_maxheaderscount"><code>server.maxHeadersCount</code></a></li> 161<li><a href="#https_server_requesttimeout"><code>server.requestTimeout</code></a></li> 162<li><a href="#https_server_settimeout_msecs_callback"><code>server.setTimeout([msecs][, callback])</code></a></li> 163<li><a href="#https_server_timeout"><code>server.timeout</code></a></li> 164<li><a href="#https_server_keepalivetimeout"><code>server.keepAliveTimeout</code></a></li> 165</ul> 166</li> 167<li><a href="#https_https_createserver_options_requestlistener"><code>https.createServer([options][, requestListener])</code></a></li> 168<li><a href="#https_https_get_options_callback"><code>https.get(options[, callback])</code></a></li> 169<li><a href="#https_https_get_url_options_callback"><code>https.get(url[, options][, callback])</code></a></li> 170<li><a href="#https_https_globalagent"><code>https.globalAgent</code></a></li> 171<li><a href="#https_https_request_options_callback"><code>https.request(options[, callback])</code></a></li> 172<li><a href="#https_https_request_url_options_callback"><code>https.request(url[, options][, callback])</code></a></li> 173</ul> 174</li> 175</ul></details> 176 177 <div id="apicontent"> 178 <h2>HTTPS<span><a class="mark" href="#https_https" id="https_https">#</a></span></h2> 179 180<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p> 181<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.19.1/lib/https.js">lib/https.js</a></p> 182<p>HTTPS is the HTTP protocol over TLS/SSL. In Node.js this is implemented as a 183separate module.</p> 184<section><h3>Class: <code>https.Agent</code><span><a class="mark" href="#https_class_https_agent" id="https_class_https_agent">#</a></span></h3> 185<div class="api_metadata"> 186<details class="changelog"><summary>History</summary> 187<table> 188<tbody><tr><th>Version</th><th>Changes</th></tr> 189<tr><td>v5.3.0</td> 190<td><p>support <code>0</code> <code>maxCachedSessions</code> to disable TLS session caching.</p></td></tr> 191<tr><td>v2.5.0</td> 192<td><p>parameter <code>maxCachedSessions</code> added to <code>options</code> for TLS sessions reuse.</p></td></tr> 193<tr><td>v0.4.5</td> 194<td><p><span>Added in: v0.4.5</span></p></td></tr> 195</tbody></table> 196</details> 197</div> 198<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 199<a href="#https_https_request_options_callback"><code>https.request()</code></a> for more information.</p> 200<h4><code>new Agent([options])</code><span><a class="mark" href="#https_new_agent_options" id="https_new_agent_options">#</a></span></h4> 201<div class="api_metadata"> 202<details class="changelog"><summary>History</summary> 203<table> 204<tbody><tr><th>Version</th><th>Changes</th></tr> 205<tr><td>v12.5.0</td> 206<td><p>do not automatically set servername if the target host was specified using an IP address.</p></td></tr> 207</tbody></table> 208</details> 209</div> 210<ul> 211<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. 212Can have the same fields as for <a href="http.html#http_new_agent_options"><code>http.Agent(options)</code></a>, and 213<ul> 214<li> 215<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. 216Use <code>0</code> to disable TLS session caching. <strong>Default:</strong> <code>100</code>.</p> 217</li> 218<li> 219<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 220<a href="https://en.wikipedia.org/wiki/Server_Name_Indication">Server Name Indication extension</a> to be sent to the server. Use 221empty string <code>''</code> to disable sending the extension. 222<strong>Default:</strong> host name of the target server, unless the target server 223is specified using an IP address, in which case the default is <code>''</code> (no 224extension).</p> 225<p>See <a href="tls.html#tls_session_resumption"><code>Session Resumption</code></a> for information about TLS session reuse.</p> 226</li> 227</ul> 228</li> 229</ul> 230<h5>Event: <code>'keylog'</code><span><a class="mark" href="#https_event_keylog" id="https_event_keylog">#</a></span></h5> 231<div class="api_metadata"> 232<span>Added in: v13.2.0, v12.16.0</span> 233</div> 234<ul> 235<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> 236<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 237generated.</li> 238</ul> 239<p>The <code>keylog</code> event is emitted when key material is generated or received by a 240connection managed by this agent (typically before handshake has completed, but 241not necessarily). This keying material can be stored for debugging, as it 242allows captured TLS traffic to be decrypted. It may be emitted multiple times 243for each socket.</p> 244<p>A typical use case is to append received lines to a common text file, which is 245later used by software (such as Wireshark) to decrypt the traffic:</p> 246<pre><code class="language-js"><span class="hljs-comment">// ...</span> 247https.<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> { 248 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> }); 249});</code></pre> 250</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> 251<div class="api_metadata"> 252<span>Added in: v0.3.4</span> 253</div> 254<ul> 255<li>Extends: <a href="tls.html#tls_class_tls_server" class="type"><tls.Server></a></li> 256</ul> 257<p>See <a href="http.html#http_class_http_server"><code>http.Server</code></a> for more information.</p> 258<h4><code>server.close([callback])</code><span><a class="mark" href="#https_server_close_callback" id="https_server_close_callback">#</a></span></h4> 259<div class="api_metadata"> 260<span>Added in: v0.1.90</span> 261</div> 262<ul> 263<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 264<li>Returns: <a href="https.html#https_class_https_server" class="type"><https.Server></a></li> 265</ul> 266<p>See <a href="http.html#http_server_close_callback"><code>server.close()</code></a> from the HTTP module for details.</p> 267<h4><code>server.headersTimeout</code><span><a class="mark" href="#https_server_headerstimeout" id="https_server_headerstimeout">#</a></span></h4> 268<div class="api_metadata"> 269<span>Added in: v11.3.0</span> 270</div> 271<ul> 272<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> 273</ul> 274<p>See <a href="http.html#http_server_headerstimeout"><code>http.Server#headersTimeout</code></a>.</p> 275<h4><code>server.listen()</code><span><a class="mark" href="#https_server_listen" id="https_server_listen">#</a></span></h4> 276<p>Starts the HTTPS server listening for encrypted connections. 277This 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> 278<h4><code>server.maxHeadersCount</code><span><a class="mark" href="#https_server_maxheaderscount" id="https_server_maxheaderscount">#</a></span></h4> 279<ul> 280<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> 281</ul> 282<p>See <a href="http.html#http_server_maxheaderscount"><code>http.Server#maxHeadersCount</code></a>.</p> 283<h4><code>server.requestTimeout</code><span><a class="mark" href="#https_server_requesttimeout" id="https_server_requesttimeout">#</a></span></h4> 284<div class="api_metadata"> 285<span>Added in: v14.11.0</span> 286</div> 287<ul> 288<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> 289</ul> 290<p>See <a href="http.html#http_server_requesttimeout"><code>http.Server#requestTimeout</code></a>.</p> 291<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> 292<div class="api_metadata"> 293<span>Added in: v0.11.2</span> 294</div> 295<ul> 296<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> 297<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 298<li>Returns: <a href="https.html#https_class_https_server" class="type"><https.Server></a></li> 299</ul> 300<p>See <a href="http.html#http_server_settimeout_msecs_callback"><code>http.Server#setTimeout()</code></a>.</p> 301<h4><code>server.timeout</code><span><a class="mark" href="#https_server_timeout" id="https_server_timeout">#</a></span></h4> 302<div class="api_metadata"> 303<details class="changelog"><summary>History</summary> 304<table> 305<tbody><tr><th>Version</th><th>Changes</th></tr> 306<tr><td>v13.0.0</td> 307<td><p>The default timeout changed from 120s to 0 (no timeout).</p></td></tr> 308<tr><td>v0.11.2</td> 309<td><p><span>Added in: v0.11.2</span></p></td></tr> 310</tbody></table> 311</details> 312</div> 313<ul> 314<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> 315</ul> 316<p>See <a href="http.html#http_server_timeout"><code>http.Server#timeout</code></a>.</p> 317<h4><code>server.keepAliveTimeout</code><span><a class="mark" href="#https_server_keepalivetimeout" id="https_server_keepalivetimeout">#</a></span></h4> 318<div class="api_metadata"> 319<span>Added in: v8.0.0</span> 320</div> 321<ul> 322<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> 323</ul> 324<p>See <a href="http.html#http_server_keepalivetimeout"><code>http.Server#keepAliveTimeout</code></a>.</p> 325</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> 326<div class="api_metadata"> 327<span>Added in: v0.3.4</span> 328</div> 329<ul> 330<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>, 331<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> 332<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> 333<li>Returns: <a href="https.html#https_class_https_server" class="type"><https.Server></a></li> 334</ul> 335<pre><code class="language-js"><span class="hljs-comment">// curl -k https://localhost:8000/</span> 336<span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span class="hljs-string">'https'</span>); 337<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>); 338 339<span class="hljs-keyword">const</span> options = { 340 <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>), 341 <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>) 342}; 343 344https.<span class="hljs-title function_">createServer</span>(options, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { 345 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>); 346 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>); 347}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code></pre> 348<p>Or</p> 349<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>); 350<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>); 351 352<span class="hljs-keyword">const</span> options = { 353 <span class="hljs-attr">pfx</span>: fs.<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'test/fixtures/test_cert.pfx'</span>), 354 <span class="hljs-attr">passphrase</span>: <span class="hljs-string">'sample'</span> 355}; 356 357https.<span class="hljs-title function_">createServer</span>(options, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { 358 res.<span class="hljs-title function_">writeHead</span>(<span class="hljs-number">200</span>); 359 res.<span class="hljs-title function_">end</span>(<span class="hljs-string">'hello world\n'</span>); 360}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">8000</span>);</code></pre> 361</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> 362</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> 363<div class="api_metadata"> 364<details class="changelog"><summary>History</summary> 365<table> 366<tbody><tr><th>Version</th><th>Changes</th></tr> 367<tr><td>v10.9.0</td> 368<td><p>The <code>url</code> parameter can now be passed along with a separate <code>options</code> object.</p></td></tr> 369<tr><td>v7.5.0</td> 370<td><p>The <code>options</code> parameter can be a WHATWG <code>URL</code> object.</p></td></tr> 371<tr><td>v0.3.6</td> 372<td><p><span>Added in: v0.3.6</span></p></td></tr> 373</tbody></table> 374</details> 375</div> 376<ul> 377<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> 378<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 379<a href="#https_https_request_options_callback"><code>https.request()</code></a>, with the <code>method</code> always set to <code>GET</code>.</li> 380<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 381</ul> 382<p>Like <a href="http.html#http_http_get_options_callback"><code>http.get()</code></a> but for HTTPS.</p> 383<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 384string, 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> 385object, it will be automatically converted to an ordinary <code>options</code> object.</p> 386<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>); 387 388https.<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> { 389 <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>); 390 <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>); 391 392 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> { 393 process.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">write</span>(d); 394 }); 395 396}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 397 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(e); 398});</code></pre> 399</section><section><h3><code>https.globalAgent</code><span><a class="mark" href="#https_https_globalagent" id="https_https_globalagent">#</a></span></h3> 400<div class="api_metadata"> 401<span>Added in: v0.5.9</span> 402</div> 403<p>Global instance of <a href="#https_class_https_agent"><code>https.Agent</code></a> for all HTTPS client requests.</p> 404</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> 405</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> 406<div class="api_metadata"> 407<details class="changelog"><summary>History</summary> 408<table> 409<tbody><tr><th>Version</th><th>Changes</th></tr> 410<tr><td>v14.18.0</td> 411<td><p>When using a <code>URL</code> object parsed username and password will now be properly URI decoded.</p></td></tr> 412<tr><td>v14.1.0</td> 413<td><p>The <code>highWaterMark</code> option is accepted now.</p></td></tr> 414<tr><td>v10.9.0</td> 415<td><p>The <code>url</code> parameter can now be passed along with a separate <code>options</code> object.</p></td></tr> 416<tr><td>v9.3.0</td> 417<td><p>The <code>options</code> parameter can now include <code>clientCertEngine</code>.</p></td></tr> 418<tr><td>v7.5.0</td> 419<td><p>The <code>options</code> parameter can be a WHATWG <code>URL</code> object.</p></td></tr> 420<tr><td>v0.3.6</td> 421<td><p><span>Added in: v0.3.6</span></p></td></tr> 422</tbody></table> 423</details> 424</div> 425<ul> 426<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> 427<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 428<a href="http.html#http_http_request_options_callback"><code>http.request()</code></a>, with some differences in default values: 429<ul> 430<li><code>protocol</code> <strong>Default:</strong> <code>'https:'</code></li> 431<li><code>port</code> <strong>Default:</strong> <code>443</code></li> 432<li><code>agent</code> <strong>Default:</strong> <code>https.globalAgent</code></li> 433</ul> 434</li> 435<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 436<li>Returns: <a href="http.html#http_class_http_clientrequest" class="type"><http.ClientRequest></a></li> 437</ul> 438<p>Makes a request to a secure web server.</p> 439<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: 440<code>ca</code>, <code>cert</code>, <code>ciphers</code>, <code>clientCertEngine</code>, <code>crl</code>, <code>dhparam</code>, <code>ecdhCurve</code>, 441<code>honorCipherOrder</code>, <code>key</code>, <code>passphrase</code>, <code>pfx</code>, <code>rejectUnauthorized</code>, 442<code>secureOptions</code>, <code>secureProtocol</code>, <code>servername</code>, <code>sessionIdContext</code>, 443<code>highWaterMark</code>.</p> 444<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 445string, 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> 446object, it will be automatically converted to an ordinary <code>options</code> object.</p> 447<p><code>https.request()</code> returns an instance of the <a href="http.html#http_class_http_clientrequest"><code>http.ClientRequest</code></a> 448class. The <code>ClientRequest</code> instance is a writable stream. If one needs to 449upload a file with a POST request, then write to the <code>ClientRequest</code> object.</p> 450<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>); 451 452<span class="hljs-keyword">const</span> options = { 453 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'encrypted.google.com'</span>, 454 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>, 455 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>, 456 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span> 457}; 458 459<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> { 460 <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>); 461 <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>); 462 463 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> { 464 process.<span class="hljs-property">stdout</span>.<span class="hljs-title function_">write</span>(d); 465 }); 466}); 467 468req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 469 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(e); 470}); 471req.<span class="hljs-title function_">end</span>();</code></pre> 472<p>Example using options from <a href="tls.html#tls_tls_connect_options_callback"><code>tls.connect()</code></a>:</p> 473<pre><code class="language-js"><span class="hljs-keyword">const</span> options = { 474 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'encrypted.google.com'</span>, 475 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>, 476 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>, 477 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>, 478 <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>), 479 <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>) 480}; 481options.<span class="hljs-property">agent</span> = <span class="hljs-keyword">new</span> https.<span class="hljs-title class_">Agent</span>(options); 482 483<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> { 484 <span class="hljs-comment">// ...</span> 485});</code></pre> 486<p>Alternatively, opt out of connection pooling by not using an <a href="#https_class_https_agent"><code>Agent</code></a>.</p> 487<pre><code class="language-js"><span class="hljs-keyword">const</span> options = { 488 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'encrypted.google.com'</span>, 489 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>, 490 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>, 491 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>, 492 <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>), 493 <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>), 494 <span class="hljs-attr">agent</span>: <span class="hljs-literal">false</span> 495}; 496 497<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> { 498 <span class="hljs-comment">// ...</span> 499});</code></pre> 500<p>Example using a <a href="url.html#url_the_whatwg_url_api"><code>URL</code></a> as <code>options</code>:</p> 501<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>); 502 503<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> { 504 <span class="hljs-comment">// ...</span> 505});</code></pre> 506<p>Example pinning on certificate fingerprint, or the public key (similar to 507<code>pin-sha256</code>):</p> 508<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>); 509<span class="hljs-keyword">const</span> https = <span class="hljs-built_in">require</span>(<span class="hljs-string">'https'</span>); 510<span class="hljs-keyword">const</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">'crypto'</span>); 511 512<span class="hljs-keyword">function</span> <span class="hljs-title function_">sha256</span>(<span class="hljs-params">s</span>) { 513 <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>); 514} 515<span class="hljs-keyword">const</span> options = { 516 <span class="hljs-attr">hostname</span>: <span class="hljs-string">'github.com'</span>, 517 <span class="hljs-attr">port</span>: <span class="hljs-number">443</span>, 518 <span class="hljs-attr">path</span>: <span class="hljs-string">'/'</span>, 519 <span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>, 520 <span class="hljs-attr">checkServerIdentity</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">host, cert</span>) { 521 <span class="hljs-comment">// Make sure the certificate is issued to the host we are connected to</span> 522 <span class="hljs-keyword">const</span> err = tls.<span class="hljs-title function_">checkServerIdentity</span>(host, cert); 523 <span class="hljs-keyword">if</span> (err) { 524 <span class="hljs-keyword">return</span> err; 525 } 526 527 <span class="hljs-comment">// Pin the public key, similar to HPKP pin-sha25 pinning</span> 528 <span class="hljs-keyword">const</span> pubkey256 = <span class="hljs-string">'pL1+qb9HTMRZJmuC/bB/ZI9d302BYrrqiVuRyW+DGrU='</span>; 529 <span class="hljs-keyword">if</span> (<span class="hljs-title function_">sha256</span>(cert.<span class="hljs-property">pubkey</span>) !== pubkey256) { 530 <span class="hljs-keyword">const</span> msg = <span class="hljs-string">'Certificate verification error: '</span> + 531 <span class="hljs-string">`The public key of '<span class="hljs-subst">${cert.subject.CN}</span>' `</span> + 532 <span class="hljs-string">'does not match our pinned fingerprint'</span>; 533 <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(msg); 534 } 535 536 <span class="hljs-comment">// Pin the exact certificate, rather than the pub key</span> 537 <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> + 538 <span class="hljs-string">'D8:3E:4C:1D:98:DB:71:E4:1A:48:03:98:EA:22:6A:BD:8B:93:16'</span>; 539 <span class="hljs-keyword">if</span> (cert.<span class="hljs-property">fingerprint256</span> !== cert256) { 540 <span class="hljs-keyword">const</span> msg = <span class="hljs-string">'Certificate verification error: '</span> + 541 <span class="hljs-string">`The certificate of '<span class="hljs-subst">${cert.subject.CN}</span>' `</span> + 542 <span class="hljs-string">'does not match our pinned fingerprint'</span>; 543 <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(msg); 544 } 545 546 <span class="hljs-comment">// This loop is informational only.</span> 547 <span class="hljs-comment">// Print the certificate and public key fingerprints of all certs in the</span> 548 <span class="hljs-comment">// chain. Its common to pin the public key of the issuer on the public</span> 549 <span class="hljs-comment">// internet, while pinning the public key of the service in sensitive</span> 550 <span class="hljs-comment">// environments.</span> 551 <span class="hljs-keyword">do</span> { 552 <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>); 553 <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>); 554 555 hash = crypto.<span class="hljs-title function_">createHash</span>(<span class="hljs-string">'sha256'</span>); 556 <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>)); 557 558 lastprint256 = cert.<span class="hljs-property">fingerprint256</span>; 559 cert = cert.<span class="hljs-property">issuerCertificate</span>; 560 } <span class="hljs-keyword">while</span> (cert.<span class="hljs-property">fingerprint256</span> !== lastprint256); 561 562 }, 563}; 564 565options.<span class="hljs-property">agent</span> = <span class="hljs-keyword">new</span> https.<span class="hljs-title class_">Agent</span>(options); 566<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> { 567 <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>); 568 <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>); 569 <span class="hljs-comment">// Print the HPKP values</span> 570 <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>]); 571 572 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> {}); 573}); 574 575req.<span class="hljs-title function_">on</span>(<span class="hljs-string">'error'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> { 576 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(e.<span class="hljs-property">message</span>); 577}); 578req.<span class="hljs-title function_">end</span>();</code></pre> 579<p>Outputs for example:</p> 580<pre><code class="language-text">Subject Common Name: github.com 581 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 582 Public key ping-sha256: pL1+qb9HTMRZJmuC/bB/ZI9d302BYrrqiVuRyW+DGrU= 583Subject Common Name: DigiCert SHA2 Extended Validation Server CA 584 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 585 Public key ping-sha256: RRM1dGqnDFsCJXBTHky16vi1obOlCgFFn/yOhI/y+ho= 586Subject Common Name: DigiCert High Assurance EV Root CA 587 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 588 Public key ping-sha256: WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18= 589All OK. Server matched our pinned cert or public key 590statusCode: 200 591headers: 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> 592 <!-- API END --> 593 </div> 594 </div> 595 </div> 596 <script> 597 'use strict'; 598 { 599 const kCustomPreference = 'customDarkTheme'; 600 const userSettings = sessionStorage.getItem(kCustomPreference); 601 const themeToggleButton = document.getElementById('theme-toggle-btn'); 602 if (userSettings === null && window.matchMedia) { 603 const mq = window.matchMedia('(prefers-color-scheme: dark)'); 604 if ('onchange' in mq) { 605 function mqChangeListener(e) { 606 document.body.classList.toggle('dark-mode', e.matches); 607 } 608 mq.addEventListener('change', mqChangeListener); 609 if (themeToggleButton) { 610 themeToggleButton.addEventListener('click', function() { 611 mq.removeEventListener('change', mqChangeListener); 612 }, { once: true }); 613 } 614 } 615 if (mq.matches) { 616 document.body.classList.add('dark-mode'); 617 } 618 } else if (userSettings === 'true') { 619 document.body.classList.add('dark-mode'); 620 } 621 if (themeToggleButton) { 622 themeToggleButton.hidden = false; 623 themeToggleButton.addEventListener('click', function() { 624 sessionStorage.setItem( 625 kCustomPreference, 626 document.body.classList.toggle('dark-mode') 627 ); 628 }); 629 } 630 } 631 </script> 632</body> 633</html> 634