【问题标题】:What is causing my deployed Heroku app to not open?是什么导致我部署的 Heroku 应用无法打开?
【发布时间】:2021-02-04 22:52:15
【问题描述】:

所以,我正在将我的聊天应用程序部署到 Heroku,并且我想测试我的 Socket.io 套接字是否正常工作。无论如何,我无法让heroku open 工作。我安装了 serve-favicon 中间件来提供帮助,但这似乎不起作用。有什么想法吗?

server.js

var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
var path = require('path');

const favicon = require('serve-favicon');
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));

let port = process.env.PORT;
if (port == null || port == "") {
    port = 8000;
}
app.listen(port);

app.get('/', (req, res) => {
   res.sendFile(__dirname + '/public/index.html');
});

io.on('connection', (socket) => {
    socket.on('userJoin', (user) => {
        console.log('User connected to room');
    })
    console.log('a user connected');
  });



http.listen(3001, () => {
  console.log('listening on port 3001');
});

过程文件

web: node server.js
web: npm start

套接字

import io from 'socket.io-client'

let socket;

export const initSocket = () => {
    socket = io('http://localhost:3001')
    console.log('initializing socket')
}

错误

2020-10-22T00:12:31.056919+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=thawing-everglades-25591.herokuapp.com request_id=6647c4af-3d55-4923-a2a1-27426e589146 fwd="184.167.78.55" dyno= connect= service= status=503 bytes= protocol=https
2020-10-22T00:12:31.317690+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=thawing-everglades-25591.herokuapp.com request_id=570c5025-8b0e-4789-b191-3783e37fac2d fwd="184.167.78.55" dyno= connect= service= status=503 bytes= protocol=https

登录 Heroku

2020-10-22T00:04:39.557182+00:00 app[web.1]: [34mℹ[39m [90m「wds」[39m: 404s will fallback to /
2020-10-22T00:04:39.557588+00:00 app[web.1]: Starting the development server...
2020-10-22T00:04:39.557590+00:00 app[web.1]: 
2020-10-22T00:04:39.698176+00:00 heroku[web.1]: Process exited with status 0
2020-10-22T00:04:39.745109+00:00 heroku[web.1]: State changed from starting to crashed
2020-10-22T00:12:31.056919+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=thawing-everglades-25591.herokuapp.com request_id=6647c4af-3d55-4923-a2a1-27426e589146 fwd="184.167.78.55" dyno= connect= service= status=503 bytes= protocol=https
2020-10-22T00:12:31.317690+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=thawing-everglades-25591.herokuapp.com request_id=570c5025-8b0e-4789-b191-3783e37fac2d fwd="184.167.78.55" dyno= connect= service= status=503 bytes= protocol=https
2020-10-22T00:18:58.529683+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=thawing-everglades-25591.herokuapp.com request_id=c349aa37-bb68-4bcc-b065-899220a3fbe8 fwd="54.83.117.30" dyno= connect= service= status=503 bytes= protocol=http
2020-10-22T00:27:34.585166+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=thawing-everglades-25591.herokuapp.com request_id=0bee0800-8f3d-4e3a-b3fe-c85e5271d1b2 fwd="184.167.78.55" dyno= connect= service= status=503 bytes= protocol=https
2020-10-22T00:27:34.856060+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=thawing-everglades-25591.herokuapp.com request_id=4ac98349-5d06-4c0d-af86-94e08a1e1193 fwd="184.167.78.55" dyno= connect= service= status=503 bytes= protocol=https

【问题讨论】:

  • 当你在heroku仪表板界面打开你的应用程序时,有一个按钮“更多”=>“查看日志”,你有什么?它应该向您显示实际错误
  • @IvanD 我已经用 Heroku 上的日志更新了我的帖子

标签: javascript reactjs express react-redux


【解决方案1】:

Heroku/Netflify 应该能够在构建时使用“npm start”构建您的应用程序,您的应用程序应该检查它是否在生产中,设置端口,并且您的服务器(用于反应)应该为构建文件夹提供服务,该文件夹将具有自己的索引.html 和所有相关文件,例如你的 favicon react 应该在构建时处理。

检查您的设置,通常像 Heroku/Netlify 这样的服务会在生产环境中传递。这是一个旧项目的 sn-p。我最近没有在 Heroku 上托管任何东西,但希望这会有所帮助。

// Serve static assests in production (serve react)
if (process.env.NODE_ENV === 'production') {
  // Set static folder
  app.use(express.static('client/build'));

  app.get('*', (req, res) => {
    res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
  });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多