服务器上的节点获取请求失败:无法获取本地颁发者证书

Cli*_*rum 6 ssl node.js express node-fetch

〜我正在使用Node 10.9.0和npm 6.2.0〜

我正在运行以下应用程序,该应用程序使我能够一遍http又一遍地向同一站点发出请求https

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...')
)
Run Code Online (Sandbox Code Playgroud)

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

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"
}
Run Code Online (Sandbox Code Playgroud)

我搜索了此错误,并尝试了一些常规修复程序,但没有任何帮助。

这些不起作用:

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

npm set strict-ssl=false

是否有其他人在共享托管提供程序(支持Node)上遇到此问题并能够使其正常工作?也许甚至有人使用FastComet?主持人的支持人员似乎也不知道该怎么办,所以我很茫然。

m1c*_*4ls 7

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

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...')
)
Run Code Online (Sandbox Code Playgroud)

注意:这会带来安全隐患,使 https 与 http 一样不安全。


omt*_*t66 7

尝试使用以下内容:

process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0
Run Code Online (Sandbox Code Playgroud)