Heroku上的Nightmare.JS导致超时

时间:2016-05-27 05:13:26

标签: node.js heroku nightmare

我正在建立一个网站,当人们在URL的请求部分将其凭据输入网站时,它会将其登录到该网站并发回有关其帐户的数据。在本地测试此代码不会产生错误,并且会很快发生。我不确定是什么导致它在服务器上超时,而不是在本地。

以下是heroku logs

的日志文件
2016-05-27T05:03:02.884921+00:00 heroku[slug-compiler]: Slug compilation finished
2016-05-27T05:03:02.884910+00:00 heroku[slug-compiler]: Slug compilation started
2016-05-27T05:03:02.853598+00:00 heroku[web.1]: Restarting
2016-05-27T05:03:02.854287+00:00 heroku[web.1]: State changed from up to starting
2016-05-27T05:03:05.766040+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2016-05-27T05:03:08.021294+00:00 heroku[web.1]: Starting process with command `node server.js`
2016-05-27T05:03:10.308664+00:00 app[web.1]: Node app is running on port 54574
2016-05-27T05:03:11.710569+00:00 heroku[web.1]: State changed from starting to up
2016-05-27T05:03:12.491140+00:00 app[web.1]: Fri, 27 May 2016 05:03:12 GMT express deprecated req.param(name): Use req.params, req.body, or req.query instead at server.js:16:21
2016-05-27T05:03:12.490421+00:00 app[web.1]: Fri, 27 May 2016 05:03:12 GMT express deprecated req.param(name): Use req.params, req.body, or req.query instead at server.js:15:21
2016-05-27T05:03:28.689866+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2016-05-27T05:03:30.932497+00:00 heroku[web.1]: Process exited with status 143
2016-05-27T05:03:42.447506+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/getGrades?username=REDACTED&password= REDACTED" host=fantasyps.herokuapp.com request_id=e3cb4c15-09af-432c-beb7-e815784c162e fwd="69.122.17.44" dyno=web.1 connect=1ms service=30001ms status=503 bytes=0

配置文件包含运行此文件的命令

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

app.set('port', (process.env.PORT || 5000));

app.use(express.static(__dirname + '/public'));

//routes go here
app.get('/getGrades', function(req, res) {
    function handleData(error, data) {
        if (error) console.log(error);
        else res.send(data);
    }
    var scrape = require('./scrape.js');
    var username = req.param('username');
    var password = req.param('password');
    scrape.scrape(username, password, handleData);
});

app.get('/', function(req, res) {
    res.send('test');
});

// start the server
app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

这里是scrape.js

var Nightmare = require('nightmare');
var vo = require('vo');
exports.scrape = vo(function*(username, password) {
    var nightmare = Nightmare({show: false});
    var info = yield nightmare
        .goto('http://ps01.bergen.org') // Go To Powerschool
        .type('[id=fieldAccount]', username) // Fill in Username Field
        .type('[id=fieldPassword]', password) // Fill in Password Field
        .click('[type=submit]') // Submit Form (Calls Hashing on click)
        .wait(200) // Wait for Page to Load (Prevents Error)
        .goto('http://ps01.bergen.org/guardian/home.html') // Go To the Grades Page
        .evaluate(function() { // Get Array like [[course info], [tri1 grades], [tri2 grades], [tri3 grades]]
            // GET COURSE NAMES AND FILTER NON-GPA AND ELECTIVE COURSES
            var courseInfo = [], tri1 = [], tri2 = [], tri3 = [], complete = [];
            var elements = document.querySelectorAll("tr td[align=left]");
            for (var i = 0; i < elements.length; i++) {
                if (elements[i].textContent.charAt(0) != "~" && (elements[i].nextElementSibling.innerHTML != "&nbsp;" && elements[i].nextElementSibling.nextElementSibling.innerHTML != "&nbsp;" && elements[i].nextElementSibling.nextElementSibling.nextElementSibling.innerHTML != "&nbsp;")) {
                    courseInfo.push(elements[i].textContent);
                    tri1.push(elements[i].nextElementSibling.textContent); // Tri 1 Grade
                    tri2.push(elements[i].nextElementSibling.nextElementSibling.textContent); // Tri 2 Grade
                    tri3.push(elements[i].nextElementSibling.nextElementSibling.nextElementSibling.textContent); // Tri 3 Grade
                }
            }
            complete.push(courseInfo);
            complete.push(tri1);
            complete.push(tri2);
            complete.push(tri3);
            return complete;
        })
        yield nightmare.end();
        return info;
});

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

因此,看起来事情已经超时,因为您在请求中运行了长时间运行的生成器。

您通常不想实时执行此类操作,而是希望为每个学生预先填写此信息,然后将其存储在数据库中,或者使用类似工具进行异步操作kue

另外,我第一眼就看到了另一个问题:

您最初点击的网址无效。您正在访问网址https://ps01.bergen.org,但您想点击网址https://ps01.bergen.org/public/

希望这有帮助!