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