2017-01-25 8 views
1

DBに挿入しようとすると、厄介なh12(タイムアウトエラー)が発生します。Heroku h12 PG/Node.jsのタイムアウトエラー

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

'/ api'ルートのルートコード全体を貼り付けることはできますか? – rdegges

+0

@rdeggesありがとうございました! – andehlu

答えて

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()の明示的な呼び出し?これは明示的に "これで、リクエストが完了し、私たちが通っているクライアントに伝えます!"

ヘロクのログに30秒のタイムアウトが表示されるのは、要求が「終了」しなかったためです。要求が30秒以内に完了しない場合、Herokuは自動的に503を返します。

+0

感謝します!私はもう少し目立つように文書化されるべきだと感じています;) – andehlu

+0

こんにちは@rdegges ..これはGETの仕組みもどうですか?私は後で私のルートファイルを取得すると同じエラーが発生しています。 router.get( '/ API'、関数(REQ、RES、次){ \t pg.connect(dbConnectの、関数(ERR、クライアントが、行わ){ \t \t client.query( 'kujadata SELECT * FROM' 、関数(ERR、結果){ \t \t \t res.render( 'API'、{タイトル: '記録データ'、geolocs:result.rows}); \t \t \t client.end(); \t \t \t完了(); \t \t}); \t}); }); – andehlu

+0

今回はresファイルをレンダリングしていますので、res.endは必要ないと思いますか? – andehlu

関連する問題