Node.js - 当此代码运行时,Heroku 服务器崩溃

标签 node.js heroku crash

我有一个在我的本地主机上完美运行的 Node.js 应用程序。

但是,当我上传到 Heroku 时,一旦执行部分代码,服务器就会崩溃。

给定的代码应该从表单传递的 URL 字符串中提取一部分。

这是导致服​​务器崩溃的代码:

  function getUserDetails(username) {
    return new Promise(done => {
        var data = [];
        https.get(`https://www.instagram.com/${username}/?__a=1`, resp => {
            resp.on('data', chunk => data.push(chunk));
            resp.on('end', () => {
                var json = JSON.parse(data.join(''));
                done(json.graphql.user);
            });
        });
    });
} 

Heroku 崩溃后 - 它仍然持续崩溃,我使用 heroku log --tail 检查了日志。

这是日志中的报告:

2019-10-06T10:59:40.782839+00:00 app[web.1]: undefined:1
2019-10-06T10:59:40.782863+00:00 app[web.1]:
2019-10-06T10:59:40.782865+00:00 app[web.1]:
2019-10-06T10:59:40.782867+00:00 app[web.1]:
2019-10-06T10:59:40.782870+00:00 app[web.1]: SyntaxError: Unexpected end of JSON
 input
2019-10-06T10:59:40.782872+00:00 app[web.1]: at JSON.parse (<anonymous>)
2019-10-06T10:59:40.782874+00:00 app[web.1]: at IncomingMessage.resp.on (/app/ap
p/routes.js:108:33)
2019-10-06T10:59:40.782876+00:00 app[web.1]: at IncomingMessage.emit (events.js:
203:15)
2019-10-06T10:59:40.782878+00:00 app[web.1]: at endReadableNT (_stream_readable.
js:1145:12)
2019-10-06T10:59:40.782880+00:00 app[web.1]: at process._tickCallback (internal/
process/next_tick.js:63:19)
2019-10-06T10:59:40.869547+00:00 heroku[web.1]: State changed from up to crashed

2019-10-06T10:59:40.847583+00:00 heroku[web.1]: Process exited with status 1
2019-10-06T10:59:40.790256+00:00 heroku[router]: at=error code=H13 desc="Connect
ion closed without response" method=POST path="/MY_PATH" host=MY_HOST.herok
uapp.com request_id=15118004-c799-45bd-a0a9-909cbd3a5e86 fwd="130.43.125.250" dy
no=web.1 connect=1ms service=101ms status=503 bytes=0 protocol=https

最佳答案

你从哪里获取url值? 看起来您正在从其他地方读取 json 输入,并且格式不正确或缺少标签等。

在本地运行您的代码片段时,它会按预期工作。

var URL = require('url').URL;

url = "stackoverflow.com/questions/58256767/node-js-heroku-server-crashes-when this-code-runs";

if(!/^https?:\/\//i.test(url)){
  url = "http://" + url;}
let parsed = new URL(url);
let retrieved = parsed.pathname.split('/')[1];

输出:

node v10.16.0

=> 'http://stackoverflow.com/questions/58256767/node-js-heroku-server-crashes-when-this-code-runs'

关于Node.js - 当此代码运行时,Heroku 服务器崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58256767/

相关文章:

iphone - 苹果拒绝了我,不知道为什么,也不知道如何解决

javascript - 在 res.render 上发送 javascript 变量 - express

node.js - 使用 Nodejs 实时抓取聊天记录

node.js - 如何在从 Node js 应用程序注销后使浏览器缓存失效

javascript - 为什么我的代码发送邮件两次?

reactjs - 混合内容与 heroku 服务器

node.js - Heroku/Node/React - 如何访问 heroku 上的 Node api 端点

ruby-on-rails - 为 heroku run rake、heroku run console 等关闭 NewRelic

java - 需要帮助分析 Java 崩溃

javascript - D3.js 中的脚本使 Chrome 崩溃