NodeJS 和 Express:“错误:自签名证书" [英] NodeJS and Express: "Error: self signed certificate"

查看:170
本文介绍了NodeJS 和 Express:“错误:自签名证书"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 NodeJS 的初学者,我有一个非常简单的 Node/Express 应用程序,它使用 PostGreSQL 作为数据库.我的db.js"文件如下所示:

I'm a beginner in NodeJS, and I have a very simple Node/Express application that uses PostGreSQL as the database. My "db.js" file looks like this:

const { Pool } = require('pg');

module.exports = new Pool({
    user: 'postgres',
    password: 'xxxx',
    host: 'localhost',
    port: 5432,
    database:'gymmanager'
});

当我只在本地运行时,一切正常,所以我决定将应用程序部署到 Heroku.为此,我创建了一个 .env 文件,其中包含用于开发的环境变量.这是 .env 文件:

When I was only running locally, everything was working fine, so I decided do deploy the app to Heroku. In order to do so, I've created a .env file with my environment variables for development. Here is the .env file:

NODE_ENV=dev
DB_USER='postgres'
DB_PASS='xxxx'
DB_HOST='localhost'
DB_PORT=5432
DB_DATABASE='gymmanager'

而且我已经更改了我的db.js"文件,现在看起来像这样:

And I have changed my "db.js" file, that now looks like this:

if(process.env.NODE_ENV !== 'dev') {

    const { Client } = require('pg');

    const client = new Client({  
        connectionString: process.env.DATABASE_URL,
        ssl: true
    });

    client.connect();
    module.exports = client;

} else {

    require('dotenv').config();
    const { Pool } = require('pg');

    module.exports = new Pool({
        user: process.env.DB_USER,
        password: process.env.DB_PASS,
        host: process.env.DB_HOST,
        port: process.env.DB_PORT,
        database: process.env.DB_DATABASE,
        rejectUnauthorized: false
    });    
}

我的应用程序已部署并在 Heroku 上顺利运行,但是现在,当我在本地运行npm start"时,出现以下错误:

My app was deployed and is running smoothly on Heroku, but now, when I run 'npm start' locally, I get the following Error:

(node:12989) UnhandledPromiseRejectionWarning: Error: self signed certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1491:34)
    at TLSSocket.emit (events.js:315:20)
    at TLSSocket._finishInit (_tls_wrap.js:933:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:691:12)

我尝试了几种解决方案...

I have tried several solutions...

忽略无效的自签名带有 https.request 的 node.js 中的 ssl 证书?

nodejs - 证书链中的错误自签名证书

但它们似乎都不起作用.有人可以帮我吗?

But none of them seems to work. Can anyone help me?

提前致谢.:)

推荐答案

你能试试,export NODE_TLS_REJECT_UNAUTHORIZED=0 在命令行上吗?这会全局设置属性,而不是 process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0; 将属性设置为该特定进程.希望你也执行了 npm config set strict-ssl false.

Can you try, export NODE_TLS_REJECT_UNAUTHORIZED=0 on the command line? This sets the property globally rather than process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0; which set the property to that particular process. Hope you also executed npm config set strict-ssl false.

这篇关于NodeJS 和 Express:“错误:自签名证书"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆