【发布时间】:2020-12-25 04:32:23
【问题描述】:
我是 Google Cloud 的新手。我已经在 Google Compute Engine [as per this tutorial] 上设置了 PostgreSQL,我已经在 Google App Engine [following this guide] 上部署了 Node.js。
我的app.js代码如下:
const express = require('express');
const pg = require('pg');
const app = express();
const PORT = process.env.PORT || 8080;
var config = 'postgresql://username:password@[VM Instance External IP]:5432/database'
var client = new pg.Client(config);
client.connect();
app.enable('trust proxy');
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res) {
res.sendFile('/index.html', {root: __dirname })
});
app.get('/look', (req, res) => {
client.query(
'SELECT * FROM users', function(err, result, fields){
if (err) throw err;
res.send(result);
}
);
});
app.listen(PORT,() => {
console.log(`Server is listening on port ${PORT}`);
});
我已经设置了以下防火墙规则:
NAME NETWORK DIRECTION PRIORITY ALLOW IP RANGES DENY DISABLED
postgres default INGRESS 1000 tcp:5432 0.0.0.0/0 False
我已编辑 postgresql.conf 以使 Postgres 能够侦听所有 IP 地址:
listen_addresses = '*'
并在 pg_hba.conf 文件中添加以下行:
host all all 0.0.0.0/0 md5
当我在我的机器上运行它时,它成功连接到 Postgres。但是,当我在 Google App Engine 上运行它时,我得到以下响应代码(意味着在通过服务器处理请求的过程中连接被关闭):
499
非常感谢您的帮助。非常感谢!
【问题讨论】:
标签: node.js postgresql google-app-engine google-cloud-platform google-compute-engine