尝试插入我的数据库时,我遇到了令人讨厌的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;
任何想法都会受到赞赏。
答案 0 :(得分:3)
啊,是的,这里的问题是你没有正确'结束'你的/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()
?这就是说明了“嘿,请求已经完成,告诉客户我们已经通过!”
你的Heroku日志显示30秒超时的原因是因为你的请求从未“结束”,如果你的请求在30秒内没有完成,Heroku将自动返回503。