title: nodejs - certificate has expired
body: <p>In my nodejs code, I request to a site using <code>request</code> library:</p>
  
  <pre><code>const httpReq = require("request")
  function postJSON(uri, data, headers) {
    if (uri.slice(-1) != "/") {
      uri += "/"
    }
  
    console.log(`[postJSON] uri: ${uri}`)
    console.log(`[postJSON] body: ${JSON.stringify(data, null, 2)}`)
    console.log("[postJSON] headers", JSON.stringify(headers, null, 2))
  
    return new Promise((resolve, reject) =&gt; {
      httpReq(
        {
          method: "POST",
          uri: uri + Math.floor(Math.random() * 100000000).toString(),
          headers: headers,
          json: data
        }, function (err, resp, body) {
          console.log(`[postJSON] done with error: ${err} -&gt; ${uri}`)
  
          if (err) {
            reject(err)
          }
          else {
            resolve(body)
          }
        }
      )
    })
  }
  </code></pre>
  
  <p>For ~30 recent hours, my NodeJS code has got this issue:</p>
  
  <pre><code>unhandledRejection at: Promise  Promise {
    &lt;rejected&gt; { Error: certificate has expired
      at TLSSocket.&lt;anonymous&gt; (_tls_wrap.js:1108:38)
      at emitNone (events.js:105:13)
      at TLSSocket.emit (events.js:207:7)
      at TLSSocket._finishInit (_tls_wrap.js:638:8)
      at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:468:38) code: 'CERT_HAS_EXPIRED' } } reason:  { Error: certificate has expired
      at TLSSocket.&lt;anonymous&gt; (_tls_wrap.js:1108:38)
      at emitNone (events.js:105:13)
      at TLSSocket.emit (events.js:207:7)
      at TLSSocket._finishInit (_tls_wrap.js:638:8)
      at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:468:38) code: 'CERT_HAS_EXPIRED' }
  </code></pre>
  
  <p>I checked: Certificate of the endpoint (which is <code>https://...</code>) by opening Safari, accessing endpoint, and confirming the certificate expired data is ~Apr, 2021</p>
  
  <p>I also tried to request the endpoint using <code>curl</code> and <code>axios</code> library from my server (the same location as my original nodejs code), the error is still thrown as above.
  I tried to request from my local machine, using the same code. It works!</p>
  
  <p>So, there could be some difference between requests from my server and those from my local machine.</p>
  
  <p>What could possibly cause this issue? How should I fix this?</p>
  
  <p>As a temporary solution, I add this: <code>process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'</code></p>
  
  <p>Thanks</p>