13

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.

2
  • did you find a solution for this? I have the same issue with aws-sdk Commented Jul 11, 2016 at 9:50
  • Had a similar issue trying to hit the google+ api on my dev machine over http whereas my production server uses https. Commented Jul 27, 2016 at 13:58

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is terribly unsafe. See the answers here for better options: stackoverflow.com/questions/31673587/…
Thanks @LouisK, i know it's not safe but it was just for testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.