代码之家  ›  专栏  ›  技术社区  ›  Clifton Labrum

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

  •  3
  • Clifton Labrum  · 技术社区  · 8 年前

    我正在运行以下应用程序,它允许我通过 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...')
    )
    

    这两种方法在我的本地计算机上都可以正常工作,并返回Typicode提供的JSON响应。但是当我在我的web主机上把它们作为节点应用程序部署时( FastComet

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

    /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

    是否有其他人在共享宿主提供程序(支持节点)上遇到此问题并能够使其正常工作?甚至可能是使用FastComet的人?东道主的支援人员似乎也不知道该怎么办,所以我不知所措。

    2 回复  |  直到 8 年前
        1
  •  10
  •   omt66    7 年前

    尝试使用以下方法:

    process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0
    
        2
  •  2
  •   m1ch4ls    8 年前

    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一样不安全。

    推荐文章