5

我正在尝试使用node-postgres (PG)连接到我在端口 5432 上运行的 localhost PostgreSQL 数据库。为此,我设置了 ngrok来隧道我的请求。

./ngrok tcp 5432

下面的代码在本地运行时有效(即使在使用 ngrok 隧道时)。当我连接到外部数据库时,它也适用于 lambda - 在我的情况下由 Heroku 托管。

'use strict';

const PG = require('pg');

// These credentials work locally
var credentials = {
    user: 'MyUsername',
    host: 'tcp://0.tcp.ngrok.io',
    database: 'MyDatabase',
    password: 'MyPassword',
    port: '12829',
    ssl: true
};

const pool = new PG.Pool(credentials);

const connectionPromise = function(){
    return new Promise(function (resolve, reject) {
        pool.connect(function (err, client, done) {
            if(err) console.log(err);
            err ? reject(err) : resolve({
                client: client,
                done: done
            })
        })
    });
};


exports.handler = Handler;

function Handler(event, context, callback) {
    context.callbackWaitsForEmptyEventLoop = false;

    console.log("Calling handler in Lambda");
    return connectionPromise().then(function (conn) {
        console.log("Success");
        callback(null);
    }).catch(function (err) {
        console.log("Error");
        callback(err);
    });
};

// Uncomment this code to run locally.
// Handler(null, {}, function(){
//     console.log("Exiting");
//     process.exit();
// });

但是,当我尝试使用 node-postgres + Ngrok 通过 Lambda 连接到我的本地主机数据库时...

错误:getaddrinfo ENOTFOUND tcp://0.tcp.ngrok.io tcp://0.tcp.ngrok.io:12829

完整的错误信息

START RequestId:3ac634ef-310e-41ab-b20f-14c86271b5d7 版本:$LATEST 2019-01-21T16:14:27.020Z 3ac634ef-310e-41ab-b20f-14c86271b5d7 在 Lambda 中调用处理程序 2019-01-21T16:4716:4 -310e-41ab-b20f-14c86271b5d7 { 错误:getaddrinfo ENOTFOUND tcp://0.tcp.ngrok.io tcp://0.tcp.ngrok.io:12829 at errnoException (dns.js:50:10) at GetAddrInfoReqWrap .onlookup [as oncomplete] (dns.js:92:26) 代码:'ENOTFOUND',errno:'ENOTFOUND',系统调用:'getaddrinfo',
主机名:'tcp://0.tcp.ngrok.io',主机: 'tcp://0.tcp.ngrok.io',
端口:12829 } 2019-01-21T16:14:27.118Z 3ac634ef-310e-41ab-b20f-14c86271b5d7 错误 2019-01-21T16:14:27.155Z 3ac634ef-310e-41ab-b20f-14c8627155 ENOTFOUND tcp://0.tcp.ngrok.io tcp://0.tcp.ngrok.io:12829","errorType":"Error","stackTrace":["errnoException (dns.js:50:10 )","GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26)"]} END RequestId: 3ac634ef-310e-41ab-b20f-14c86271b5d7 REPORT RequestId: 3ac634ef-310e-41ab-b20f-14c86271b5d7 持续时间: 136.26毫秒计费持续时间:200 毫秒内存大小:128 MB 使用的最大内存:23 MB

lambda 阻止 ngrok 吗?

4

1 回答 1

4

tcp://从 ngrok 主机名中删除:

var credentials = {
    user: 'MyUsername',
    host: '0.tcp.ngrok.io',
    database: 'MyDatabase',
    password: 'MyPassword',
    port: '12829',
    ssl: true
};
于 2019-05-11T18:43:17.027 回答