【问题标题】:Issue in connecting to heroku postgres from node + express + also locally从节点+快递+也在本地连接到heroku postgres的问题
【发布时间】:2020-07-26 11:26:07
【问题描述】:
  • 这里是 Node 新手,最近连续尝试了 3 天,没有任何线索。
  • 阅读所有类似的问题并尝试了我能找到的所有内容,但没有运气。
  • 怀疑与我的机器或代码不常见或不相关的东西。

  • 问题:尝试从 postgres db 获取 node.js 中的数据 - 到控制台(至少)以便稍后将其呈现为 HTML

  • 数据库有表名:heroku 上的students,并且有记录

  • 同样在我的 macOS 本地,我在名为 students 的表中安装了带有简单数据的 postgres

  • 我无法获取任何数据,没有错误,不知道如何跟踪它!

  • 尝试创建与池、客户端的连接.. 也使用了 heroku 指南 here 准确

  • 其他用户最常遇到的所有内容

  • DATABASE_URL 环境变量没问题,如果我在终端中回显 $DATABASE_URL:


postgres://xnlkikdztxosk:kuadf76d555dfab0a6c159b8404e2ac254f581639c09079baae4752a7b64a@ec3-52-120-48-116.compute-1.amazonaws.com:5432/uytnmb7fvbg1764

  • 当我运行“node app.js”时,服务器在端口 3000 上启动正常,我可以在根目录“/”上使用邮递员,它可以正常工作,它返回 json 信息和 console.log
  • 如果我尝试邮递员到 '/students' 然后它会一直尝试,没有结果,没有错误,什么都没有
  • 也试过我本地安装的 postgres,同样的事情
  • 我的模块没问题,我运行 npm install 几次
  • 可能是我的 mac 防火墙,我完全关闭了它
  • 也试过这个,没有打印出来或不知道在哪里跟踪它:

process.on('uncaughtException', function (err) {
  console.log(err);
}); 

  • 我们将非常感谢您为跟踪此类问题所遵循的指南或步骤

    app.js file:
    
      const express = require('express')
      const bodyParser = require('body-parser')
      const { Client } = require('pg');
    
      const app = express()
      const PORT = 3000
    
      app.use(bodyParser.json())
      app.use(
        bodyParser.urlencoded({
          extended: true,
        })
      )
    
      const client = new Client({
        connectionString: process.env.DATABASE_URL,
        ssl: {
          rejectUnauthorized: false
        }
      });
      client.connect();
    
      app.get('/', (req, res) => {
        res.json({ info: 'Info: This is the root directory' });
        console.log('main directory')
      })
    
      app.get('/students', (req, res) => {
    
        client.query('SELECT * FROM students;', (err, res) => {
          if (err) throw err;
          for (let row of res.rows) {
            console.log(JSON.stringify(row));
            console.log('WHOOOOOO, finally!');
          }
          client.end();
        });
      });
    
      app.listen(PORT, function(){ 
        console.log('Server running on port 3000');
      });
    

【问题讨论】:

    标签: node.js postgresql express heroku


    【解决方案1】:

    好吧,我的 node 版本出于某种原因是 v14,不知道是怎么回事,但是 node 站点中最稳定的版本是 12,所以我安装了 v12,并且与 pg 的连接在 heroku 上本地和远程工作。

    • 这只是为了强调在连续尝试 4 天后对我有用的方法。

    但是,这可能会为您触发像我面临的不同问题:

    DeprecationWarning: Implicit disabling of certificate verification is deprecated and will be removed in pg 8. Specify `rejectUnauthorized: true` to require a valid CA or `rejectUnauthorized: false` to explicitly opt out of MITM protection.
    
    • 到目前为止找到的所有答案都指向:pg 模块已在 v7.18.1 中修复,但由于某种原因我无法强制 package.json 采用该版本,它会将我跳转到版本 7.18.2
    • 尝试了与最新版本 8.3 一起与 heroku 相同的问题,但在本地没有显示消息

    不过没什么大不了的,连接现在可以正常工作,直到弄清楚为止。

    【讨论】:

      【解决方案2】:

      我认为这里的问题是您没有在/students 路由中发回任何响应。注意/ 路由你有一个res.json 发回响应但在/students 路由我不'看不到您的回复发送到哪里,这就是您永远等待的原因

      【讨论】:

      • 响应资源在代码中..发现问题,在下面。
      猜你喜欢
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      • 2013-02-05
      • 2021-06-08
      • 2018-05-19
      相关资源
      最近更新 更多