最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Encountering Error: self-signed certificate in certificate chain - Stack Overflow

programmeradmin4浏览0评论

I encountered this error while trying to set up proxy Error: self-signed certificate in certificate chain. Does anyone know why this happened? I have downloaded the SSL certificate from the proxy provider and linked it but to no avail. This is my code:

import fs from 'fs';
import { HttpsProxyAgent } from 'https-proxy-agent';
import fetch from 'node-fetch';

const ca = fs.readFileSync('../certs/ca.crt');

const brd_user = '';
const brd_zone = '';
const brd_passwd = '';
const port = '';
const brd_superpoxy = `brd.superproxy.io:${port}`;
const brd_connectStr = 'brd-customer-' + brd_user + '-zone-' + brd_zone + ':' + brd_passwd + '@' + brd_superpoxy;

const agent = new HttpsProxyAgent( 'http://' + brd_connectStr, {
    ca: ca, //attach cert
});

const brd_test_url = '/';

(async function fetchData() {
    try {
        const response = await fetch(brd_test_url, {
            agent, //Use proxy 
        });

        const data = await response.text(); //get response
        console.log(data);
    } catch (error) {
        console.error('Error:', error);
    }
})();

However, the code given in the example works:

var fs = require ('fs');
//load cert
const ca = fs.readFileSync('./certs/ca.pem');

//Bright Data Access
const brd_user = '';
const brd_zone = '';
const brd_passwd = '';
const brd_superpoxy = 'brd.superproxy.io:33335';
const brd_connectStr = 'brd-customer-' + brd_user + '-zone-' + brd_zone + ':' + brd_passwd + '@' + brd_superpoxy;


//Switch between brd_test_url to get a json instead of txt response: 
// const brd_test_url = '.json';

const brd_test_url = '';

require('request-promise')({
    url: brd_test_url,
    proxy: 'http://' + brd_connectStr,
    //add cert in the request options
    ca: ca,
    })
.then(function(data){ console.log(data); },
    function(err){ console.error(err); });

Why did one work and the other didn't?

I tried export NODE_EXTRA_CA_CERTS=<Cert file path> but it still didn't work, which is expected. I saw other solutions involving setting rejectUnauthorized: false but is there other alternatives?

发布评论

评论列表(0)

  1. 暂无评论