Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to verify the first certificate in Node.js

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.

I receive the following error when making a GET request:

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

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.

like image 910
Sideeq Youssef Avatar asked Jun 01 '16 11:06

Sideeq Youssef


1 Answers

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.

like image 76
Sideeq Youssef Avatar answered Sep 21 '22 21:09

Sideeq Youssef