无法验证 Node.js 中的第一个证书

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37567148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:16:24  来源:igfitidea点击:

Unable to verify the first certificate in Node.js

node.jsexpresssslrequestssl-certificate

提问by Sideeq Youssef

I have a Nodejs API and it uses ssl and https, so i'm trying to consume it on a different server to build a web app using express-js.

我有一个 Nodejs API,它使用 ssl 和 https,所以我试图在不同的服务器上使用它来使用 express-js 构建一个 Web 应用程序。

I receive the following error when making a GET request:

发出 GET 请求时收到以下错误:

events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: unable to verify the first certificate
    at Error (native)
    at TLSSocket.<anonymous> (_tls_wrap.js:1017:38)
    at emitNone (events.js:67:13)
    at TLSSocket.emit (events.js:166:7)
    at TLSSocket._init.ssl.onclienthello.ssl.oncertcb.TLSSocket._finishInit (_tls_wrap.js:582:8)
    at TLSWrap.ssl.onclienthello.ssl.oncertcb.ssl.onnewsession.ssl.onhandshakedone (_tls_wrap.js:424:38)

I've tried the following, with no success:

我尝试了以下方法,但没有成功:

  • Adding require('ssl-root-cas').inject();and rejectUnauthorized: false,on the request.

  • Adding process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";on my main file, which gave me a Error: socket hang up

  • 在请求中添加require('ssl-root-cas').inject();rejectUnauthorized: false,

  • 添加process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";我的主文件,这给了我一个Error: socket hang up

The request code:

请求代码:

var service_bus = require('service_bus');
var error_engine = require('error_engine');

exports.get = function(call_back){

    function request_service() {
        require('ssl-root-cas').inject();

    var end_point = {
        host: "www.xxxx.com",
        port: "4433",
        path: "/api/xxx/xx/",
        method: "GET",
        headers: {
            "Content-Type": "application/json",
            "X-app-key": "xxxxxxxxxxxxxxxxxxxxxxxxxx"
        },
        is_ssl: true
    };


 service_bus.call(end_point, {}, function (error, result) {
     console.log(error);
        if (!error) {
             return call_back(result);
        }
        else {
            return call_back(error_engine.get_error_by_code('1500', ''));
        }
    });
}

request_service();

};

and the main file for the web app:

和网络应用程序的主文件:

var express  = require('express');
var exphbs   = require('express-handlebars');
var path     = require('path');
var passport = require('passport');
var session  = require('express-session');
var uuid     = require("node-uuid");
var cookieParser = require('cookie-parser');
var bodyParser   = require('body-parser');
var mongoose = require('mongoose');
var app      = express();

app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');

app.use(cors());
app.use(express.static(__dirname + '/public'));
app.use('img',express.static(path.join(__dirname, 'public/images')));
app.use('js',express.static(path.join(__dirname, 'public/js')));
app.use('css',express.static(path.join(__dirname, 'public/css')));
app.use('fonts',express.static(path.join(__dirname, 'public/fonts')));
//app.use(exphbs.registerHelper('paginate', paginate));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(passport.initialize());
app.use(passport.session());
app.use(session({
    proxy: true,
    resave: true,
    saveUninitialized: true,
    uuid: function(req) {
        return uuid()
    },
    secret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
}));


require("./xxxxxxx/endpoints")(app);
require("./xxxxxxxx/endpoints")(app);
require("./xxxxxxxx/endpoints")(app, passport);

mongoose.connect("mongodb://localhost/database");
app.listen(8080);

Any suggestions as to why this error occurs are highly appreciated.

非常感谢有关为什么会发生此错误的任何建议。

采纳答案by Sideeq Youssef

I solve this problem by using another package it's called "request" also don't forget to add

我通过使用另一个名为“请求”的包解决了这个问题,也不要忘记添加

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

before calling the API.

在调用 API 之前。