csbukhari csbukhari - 4 months ago 528
Node.js Question

[Nodejs]: unable to verify the first certificate

I have a Nodejs API and it use ssl and https, so i'm trying to consume it on different server to build an web app using expressjs and i face this issues when i try to send 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 try to solve it using many solutions but still it's not work.

this some of the solutions i try but still not work.

i try to use

require('ssl-root-cas').inject();


and try to add
rejectUnauthorized: false,
on the request

also i try to add
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
on my main file but it give me another error
Error: socket hang up


this 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 this is my 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);

Answer

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.