0

I use this script to connect node.js with Azure Postgresql. But the ssl verification of our firewall blocks the connection, so in the past I need to use a proxy. Where in the code can I add the proxy settings as like host and port? Means when I start the code, vscode should connect through the proxy to postgresql.

const pg = require('pg');

const config = {
    host: '<your-db-server-name>.postgres.database.azure.com',
    // Do not hard code your username and password.
    // Consider using Node environment variables.
    user: '<your-db-username>',     
    password: '<your-password>',
    database: '<name-of-database>',
    port: 5432,
    ssl: true
};

const client = new pg.Client(config);

client.connect(err => {
    if (err) throw err;
    else { queryDatabase(); }
});

function queryDatabase() {
  
    console.log(`Running query to PostgreSQL server: ${config.host}`);

    const query = 'SELECT * FROM inventory;';

    client.query(query)
        .then(res => {
            const rows = res.rows;

            rows.map(row => {
                console.log(`Read: ${JSON.stringify(row)}`);
            });

            process.exit();
        })
        .catch(err => {
            console.log(err);
        });
}
4

1 回答 1

1

为 Visual Studio Code 配置代理

编辑 settings.json 文件

根据您的平台,用户设置文件位于此处:

Windows: %APPDATA%\Code\User\settings.json

macOS: $HOME/Library/Application Support/Code/User/settings.json

Linux: $HOME/.config/Code/User/settings.json

修改并添加以下行以配置您的代理

"http.proxy": "http://user:pass@proxy.com:portnumber",
"https.proxy": "http://user:pass@proxy.com:portnumber",
"http.proxyStrictSSL": false

如果您的代理不需要身份验证,您可以简单地使用

"http.proxy": "http://proxy.com:portnumber",
"https.proxy": "http://proxy.com:portnumber"
"http.proxyStrictSSL": false

重启 VS 代码

与 settings.json 文件的设置和架构相关的文档在这里供参考

于 2020-08-31T07:56:53.393 回答