如何避免heroku请求超时?

时间:2015-06-20 22:03:02

标签: node.js heroku heroku-toolbelt

我已将node.js服务部署到Heroku但面临请求超时错误: https://devcenter.heroku.com/articles/error-codes#h12-request-timeout

如何避免这种情况或解决此问题?这是node.js服务中的错误,还是可以在Heroku Dyno设置中更改它?

这显示在日志中:

2015-06-21T00:48:01.965583+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/" host=damp-meadow-5935.herokuapp.com request_id=4ca2e2a9-b983-4cec-a8e1-cd7e73c5b274 fwd="54.157.11.252" dyno=web.1 connect=2ms service=30007ms status=503 bytes=0

我的node.js服务如下所示:

// set up ======================================================================
var express = require('express');
var app = express();                               // create our app w/ express
var port = process.env.PORT || 5001;                // set the port

var bodyParser = require('body-parser');
var methodOverride = require('method-override')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

app.use(function (req, res, next) {
    //the code hits this point!
    var data = '';
    req.on('data', function (chunk) {
        data += chunk;
    });
    req.on('end', function () {
        req.rawBody = data;
        next();
    });
    //console.log(data);
});
app.use(methodOverride());
// routes ======================================================================
require('./routes.js')(app);

var server = app.listen(port);

var _ = require('underscore');

module.exports = function (app) {
    app.post('/', function (req, res) {
        console.log('testing entering /api/filter');
        var Validator = require('jsonschema').Validator;
        var v = new Validator();
        var jso = req.rawBody;
        var newjso = JSON.parse(req.rawBody);
        var schema={
            "description": "A payload",
            "type": "object"
        }

        var result = v.validate(newjso,schema);

        if (result.errors.length > 0) {
            res.setHeader('content-type', 'application/json');
            res.status(400);
            res.json({
                "error": "Could not decode request: JSON parsing failed"
            });
        }
        else
        {
            var resp = _.filter(_.where(newjso.payload, {drm: true}), function (item) {
                return item.episodeCount > 0
            });

            var newArray = [];
            resp.forEach(function (item) {
                var newItem = _.pick(item, 'image', 'slug', 'title');
                newItem.image = _.propertyOf(newItem.image)('showImage');
                newArray.push(newItem);
            })

            res.setHeader('content-type', 'application/json');
            res.status(200);
            res.json(newArray);
        }

        console.log('testing einde');    })
}

0 个答案:

没有答案