无法使用loopbackjs验证nodejs中的第一个证书

时间:2018-12-31 07:09:40

标签: javascript node.js ssl nginx loopbackjs

在下面的代码中,我试图检查UUID是否已经生成。我尝试使用https.request而不是https.get来检查是否可以解决错误,但结果是相同的。另外,我在key中使用了crt中的https.requestloopback SSL config来解决问题,但没有出现任何区别。

const options = {
   hostname: 'app.mydomain.com', //same as localhost:3000
   port: 443,
   path: '/api/uuids?filter[where][uuid]=' + UUID,
   method: 'GET',

   key: fs.readFileSync(path.join(__dirname, './../../server/ssl/domain.key')),
   cert: fs.readFileSync(path.join(__dirname, './../../server/ssl/domain.crt'))
 };
 console.log(options);
 const req = https.request(options, (res) => {

 res.on('data', (d) => {
d = JSON.parse(d)
            console.log(d);
            if (d.length == 1 && d[0].uuid == UID) {

                return 200;
            }
            else {
                return 500;
            }
   });
 });

 req.on('error', (e) => {
   console.error(e);
 });

控制台错误:

(node:4452) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated
{ Error: unable to verify the first certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1049:34)
    at TLSSocket.emit (events.js:182:13)
    at TLSSocket._finishInit (_tls_wrap.js:631:8) code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' }

我也尝试使用:

    const https = require('https').globalAgent.options.ca = require('ssl-root-cas/latest').create();

In the declaration of `https` but the following error appears:

(node:4500) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated
Unhandled error for request POST /registrations: TypeError: https.request is not a function
    at checkUUID (C:\apps\app\common\models\registration.js:60:20)
    at C:\apps\app\common\models\registration.js:16:17
    at notifySingleObserver (C:\apps\app\node_modules\loopback-datasource-juggler\lib\observer.js:160:22)
    at C:\apps\app\node_modules\async\dist\async.js:3110:16
    at replenish (C:\apps\app\node_modules\async\dist\async.js:1011:17)
    at C:\apps\app\node_modules\async\dist\async.js:1016:9
    at eachLimit$1 (C:\apps\app\node_modules\async\dist\async.js:3196:24)
    at Object.<anonymous> (C:\apps\app\node_modules\async\dist\async.js:1046:16)
    at doNotify (C:\apps\app\node_modules\loopback-datasource-juggler\lib\observer.js:157:11)
    at doNotify (C:\apps\app\node_modules\loopback-datasource-juggler\lib\observer.js:155:49)
    at doNotify (C:\apps\app\node_modules\loopback-datasource-juggler\lib\observer.js:155:49)
    at doNotify (C:\apps\app\node_modules\loopback-datasource-juggler\lib\observer.js:155:49)
    at Function.ObserverMixin._notifyBaseObservers (C:\apps\app\node_modules\loopback-datasource-juggler\lib\observer.js:178:5)
    at Function.ObserverMixin.notifyObserversOf (C:\apps\app\node_modules\loopback-datasource-juggler\lib\observer.js:153:8)
    at Function.ObserverMixin._notifyBaseObservers (C:\apps\app\node_modules\loopback-datasource-juggler\lib\observer.js:176:15)
    at Function.ObserverMixin.notifyObserversOf (C:\apps\app\node_modules\loopback-datasource-juggler\lib\observer.js:153:8)

其他信息

  • 我将个人服务器用作vps /专用服务器
  • 我正在将Nginx与rever一起用于app.mydomain.com mydomain.com + wwww.mydomain.com + app.mydomain.com均可通过以下方式访问 仅sslport 443
  • 我使用 zerossl 来获取我的域和订阅的证书。
  • 我使用了与该网站生成的相同的keycert
    loopbackjs ssl config

1 个答案:

答案 0 :(得分:1)

两天后,我找到了解决方法,我使用了以下代码:

var rootCas = require('ssl-root-cas/latest').create();
rootCas.addFile(__dirname +  './../../server/ssl/domain.crt');
const https = require('https');//.globalAgent.options.ca = require('ssl-root-cas/latest').create();
var request = require('request');

 const options = {
   hostname: 'app.mydomain.com',
   port: 443,
   path: '/api/uuids?filter[where][uuid]=' + UID,
   method: 'GET',
   key: fs.readFileSync(path.join(__dirname, './../../server/ssl/domain.key')),
   cert: fs.readFileSync(path.join(__dirname, './../../server/ssl/domain.crt'))
 };
 const req = https.get(options, (res) => {
    res.on('data', (d) => {
    d = JSON.parse(d)
    if (d.length == 1 && d[0].uuid == UID) {

        return 200;
    }
    else {
        return 500;
    }
});
 });

 req.on('error', (e) => {
   console.error(e);
 });