【问题标题】:Heroku h12 Timeout Error with PG / Node.jsPG / Node.js 的 Heroku h12 超时错误
【发布时间】:2017-06-10 00:44:26
【问题描述】:

我在尝试插入我的数据库时遇到令人讨厌的 h12(超时错误)。

2017-01-25T00:22:24.764591+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/api" host=kujaflow.herokuapp.com request_id=fb69dfdf-12ef-4dc8-8feb-45bef0c26746 fwd="71.125.214.77" dyno=web.1 connect=0ms service=30005ms status=503 bytes=0

我不知道为什么会这样。从我收集的内容来看,我的节点代码看起来不错。我已经编辑了用户/通行证信息....

var express = require('express');
var pg = require('pg');

var dbuser = 'xxxxxxxxx';
var dbpassword = 'xxxxxxxxx';
var dbname = 'xxxxxxxxx';
var dbhost = 'xxxxxxxxx';
var dbport = 5432;

var dbconnect = {
    user: dbuser, 
    password: dbpassword,
    host: dbhost,
    database: dbname, 
    port: dbport
};

var router = express.Router();


/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});


/* api */
router.post('/api', function(req, res, next) {

    var latitude = req.body.location.coords.latitude;
    // console.log("latitude: " + latitude);
    var longitude = req.body.location.coords.longitude;
    var heading = req.body.location.coords.heading;
    var timestamp = req.body.location.timestamp;
    var uuid = req.body.location.uuid;
    var is_moving = req.body.location.is_moving;

    pg.connect(dbconnect, function(err, client, done) {

        client.query('INSERT INTO kujadata (latitude, longitude) VALUES ($1, $2);',[latitude, longitude], function (err, result) {
            client.end();
        });

    });

});


router.get('/api', function(req, res, next) {
  res.render('index', { title: 'API' });
});

module.exports = router;

任何想法将不胜感激。

【问题讨论】:

  • 您可以粘贴您的/api 路线的全部路线代码吗?
  • 更新@rdegges 谢谢!

标签: node.js postgresql heroku


【解决方案1】:

啊,是的,这里的问题是你没有正确地“结束”你的/api 路由。当您使用 express 时,您需要显式地向客户端发送响应。

你可以这样重写它:

router.post('/api', function(req, res, next) {

  var latitude = req.body.location.coords.latitude;
  var longitude = req.body.location.coords.longitude;
  var heading = req.body.location.coords.heading;
  var timestamp = req.body.location.timestamp;
  var uuid = req.body.location.uuid;
  var is_moving = req.body.location.is_moving;

  pg.connect(dbconnect, function(err, client, done) {
    client.query('INSERT INTO kujadata (latitude, longitude) VALUES ($1, $2);', [latitude, longitude], function (err, result) {
      client.end();
      return res.end();
    });
  });

});

注意到对res.end() 的显式调用了吗?这就是告诉 express “嘿,请求完成了,告诉客户我们通过了!”

您的 Heroku 日志显示 30 秒超时的原因是因为您的请求从未“结束”,如果您的请求未在 30 秒内完成,Heroku 将自动返回 503。

【讨论】:

  • 谢谢!我觉得应该更突出地记录一下;)
  • 嗨@rdegges..这对GET也有什么作用?我的路线文件中的 get later 遇到了同样的错误。 router.get('/api', function(req, res, next) { pg.connect(dbconnect, function(err, client, done) { client.query('SELECT * FROM kujadata', function(err, result) { res.render('api',{title: '记录数据', geolocs: result.rows }); client.end(); done(); }); }); });
  • 我这次渲染的是 res 文件,所以我猜 res.end 是不需要的?
  • 正确——如果您使用的是res.render()res.json()等——则不需要res.end()。您基本上需要以某种方式致电res 才能结束请求。
猜你喜欢
  • 1970-01-01
  • 2012-07-08
  • 1970-01-01
  • 2012-06-14
  • 2021-05-10
  • 2017-01-18
  • 2020-07-09
  • 2019-12-15
  • 2020-01-26
相关资源
最近更新 更多