【问题标题】:Getting "Failed to load resource: net::ERR_CONNECTION_REFUSED" after uploading React/Node/Express app to Heroku将 React/Node/Express 应用程序上传到 Heroku 后出现“加载资源失败:net::ERR_CONNECTION_REFUSED”
【发布时间】:2020-01-24 00:21:39
【问题描述】:

如标题所述,我的网站出现“加载资源失败:net::ERR_CONNECTION_REFUSED”错误。

还有“TypeError:获取失败”和“未检查的 runtime.lastError:消息端口在收到响应之前关闭。”出于某种原因在控制台日志中。

此外,在 Heroku 上的构建日志中,它也显示“NPM_CONFIG_LOGLEVEL=error”。

据我了解,当端口没有监听任何内容时会发生这种情况。但我不确定这是怎么发生的,或者为什么会这样。

我尝试使用 process.env.NODE 而不是 env.PORT 切换端口(将其从端口 5000 更改为 3000 等)。没有变化。

这一切都基于这个 Pusher 新闻提要应用教程。这是我的 server.js 类的一些代码(假设这就是我所需要的)。

app.get('/live', (req, res) => {
    const topic = 'ipo';
    fetchNews(topic, 1)
        .then(response => {
            res.json(response.articles);
            updateFeed(topic);
        })
        .catch(error => console.log(error));
});

app.set('port', process.env.PORT || 5000);
const server = app.listen(app.get('port'), () => {
    console.log('Express running -> PORT ${server.address().port}');
});

另外,我的 app.js 代码的一小部分

componentDidMount() {
    fetch('http://localhost:5000/live')
      .then(response => response.json())
      .then(articles => {
        this.setState({
          newsItems: [...this.state.newsItems, ...articles],
        });
      }).catch(error => console.log(error));

我不确定到底发生了什么。

【问题讨论】:

    标签: node.js reactjs express heroku deployment


    【解决方案1】:

    问题在于您获取http://localhost:5000/live 的方式,而 Heroku 不是您的本地主机,它怎么知道本地主机是什么?

    【讨论】:

    • 是的,我考虑了这一点,并决定尝试放置实际的网站链接本身。但这没有用。应该有什么?
    • 我应该放什么?
    • 尝试这样做:将您的 express 应用与 react 分开提供。例如:expressapp.herokuapp.com 然后在您的反应应用程序中从expressapp.herokuapp.com/live 获取它应该可以工作。
    猜你喜欢
    • 2020-04-23
    • 2018-02-08
    • 2020-11-18
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    相关资源
    最近更新 更多