【问题标题】:Node Fetch Request Fails on Server: Unable to Get Local Issuer Certificate服务器上的节点获取请求失败:无法获取本地颁发者证书
【发布时间】:2019-01-30 10:20:25
【问题描述】:

~ 我使用的是 Node 10.9.0 和 npm 6.2.0 ~

我正在运行以下应用程序,它允许我通过httphttps 向同一站点发出请求。

var fetch = require('node-fetch')
const express = require('express')
const app = express()

//-- HTTP --
app.get('/test-no-ssl', function(req, res){
  fetch('http://jsonplaceholder.typicode.com/users')
  .then(res => res.json())
  .then(users => {
    res.send(users)
  }).catch(function(error) {
    res.send(error)
  })
})

//-- HTTPS --
app.get('/test-ssl', function(req, res){
  fetch('https://jsonplaceholder.typicode.com/users')
  .then(res => res.json())
  .then(users => {
    res.send(users)
  }).catch(function(error) {
    res.send(error)
  })
})

app.listen(3003, () => 
  console.log('Listening on port 3003...')
)

这两种方法都可以在我的本地机器上正常工作,并返回 Typicode 提供的 JSON 响应。但是当我在我的网络主机 (FastComet) 上将它们部署为 Node 应用程序时,我得到以下结果:

HTTP /test-no-ssl - 按预期返回 JSON

HTTPS /test-ssl - 返回以下错误:

{ 
  "message" : "request to https://jsonplaceholder.typicode.com/users failed, reason: unable to get local issuer certificate",
  "type" : "system",
  "errno" : "UNABLE_TO_GET_ISSUER_CERT_LOCALLY",
  "code" : "UNABLE_TO_GET_ISSUER_CERT_LOCALLY"
}

我搜索了这个错误并尝试了一些常用的修复方法,但没有任何帮助。

这些不起作用:

npm config set registry http://registry.npmjs.org/

npm set strict-ssl=false

有没有其他人在共享托管服务提供商(支持 Node)上遇到过这个问题并且能够让它工作?也许甚至是使用 FastComet 的人?楼主的后勤人员好像也不知道怎么办,我很茫然。

【问题讨论】:

    标签: node.js express ssl node-fetch


    【解决方案1】:

    尝试使用以下方法:

    process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0
    

    【讨论】:

    • 谢谢!我应该更新我的问题,因为这正是我不久前修复这个问题时所需要的。在我的代码中,它有这样的对象表示法:process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
    • 更好地使用此解决方法使其更具体地针对请求,而不是为所有请求敞开大门。 stackoverflow.com/a/59944400/8683679
    【解决方案2】:

    托管可能与证书颁发机构列表存在一些问题...作为一种解决方法,您可以尝试忽略证书有效性。

    const fetch = require('node-fetch')
    const https = require('https')
    const express = require('express')
    const app = express()
    
    const agent = new https.Agent({
      rejectUnauthorized: false
    })
    
    //-- HTTP --
    app.get('/test-no-ssl', function(req, res){
      fetch('http://jsonplaceholder.typicode.com/users')
        .then(res => res.json())
        .then(users => {
          res.send(users)
        }).catch(function(error) {
        res.send(error)
      })
    })
    
    //-- HTTPS --
    app.get('/test-ssl', function(req, res){
      fetch('https://jsonplaceholder.typicode.com/users', { agent })
        .then(res => res.json())
        .then(users => {
          res.send(users)
        }).catch(function(error) {
        res.send(error)
      })
    })
    
    app.listen(3003, () =>
      console.log('Listening on port 3003...')
    )
    

    注意:这有安全隐患,使 https 和 http 一样不安全。

    【讨论】:

    • 感谢您的回复。我尝试添加rejectUnauthorized 位,但没有帮助。奇怪的是,如果我使用一个名为 curlrequest 的节点模块,我可以成功调用上述https URL。所以我想我的网络主机一定有一些时髦的事情发生,curl 是允许的,但其他http 请求是不允许的。 ¯\_(ツ)_/¯
    • 这里相同;卷曲的作品。我认为 curl 信任的根证书与 Node.js 信任的根证书之间存在差异。如果 curl 可以,我想了解有关为什么 Node.js 无法发出请求的更多信息。
    猜你喜欢
    • 2016-04-20
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 2020-08-06
    • 2020-08-10
    相关资源
    最近更新 更多