在NodeJS中使用Socket.IO可以在本地工作,但不能在Heroku上工作

时间:2019-07-05 04:37:40

标签: javascript node.js heroku websocket socket.io

Im托盘创建用于Android客户端的NodeJs服务器。当我在本地运行服务器时,它运行完美!但是,当我尝试在Heroku中托管服务器时,它不起作用。我收到应用程序错误“应用程序中发生错误,无法为您的页面提供服务。如果您是应用程序所有者,请检查日志以获取详细信息。您可以使用命令从Heroku CLI执行此操作”。 当我从index.js中取出<>行时,它在Heroku上也有效。

在日志中,我得到一个H10错误: 2019-07-05T04:09:20.917720 + 00:00 heroku [router]:at =错误代码= H10 desc =“应用程序崩溃” method = GET path =“ /” ....

2019-07-05T04:09:21.119408 + 00:00 heroku [router]:at =错误代码= H10 desc =“应用程序崩溃” method = GET path =“ / favicon.ico” ....

出什么问题了?非常感谢!

Index.js

const express = require('express');
app = express(),
    http = require('http'),
    server = http.createServer(app);

    // without this line it works also on Heroku
    io = require('socket.io').listen(server); 

const path = require('path')
const PORT = process.env.PORT || 5000

express()
    .use(express.static(path.join(__dirname, 'public')))
    .set('views', path.join(__dirname, 'views'))
    .set('view engine', 'ejs')
    .get('/', (req, res) => res.render('pages/index'))
    .listen(PORT, () => console.log(`Listening on ${ PORT }`))

package.json

{
  "name": "node-js-getting-started",
  "version": "0.3.0",
  "description": "A sample Node.js app using Express 4",
  "engines": {
    "node": "10.x"
  },
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "node test.js"
  },
  "dependencies": {
    "ejs": "^2.5.6",
    "express": "^4.15.2"
  },
  "devDependencies": {
    "request": "^2.81.0",
    "tape": "^4.7.0"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/heroku/node-js-getting-started"
  },
  "keywords": [
    "node",
    "heroku",
    "express"
  ],
  "license": "MIT"
}

app.json

{
  "name": "Start on Heroku: Node.js",
  "description": "A barebones Node.js app using Express 4",
  "repository": "https://github.com/heroku/node-js-getting-started",
  "logo": "https://cdn.rawgit.com/heroku/node-js-getting-started/master/public/node.svg",
  "keywords": ["node", "express", "heroku"],
  "image": "heroku/nodejs"
}

gitignore

# Node build artifacts
node_modules
npm-debug.log

# Local development
*.env
*.dev
.DS_Store

# Docker
Dockerfile
docker-compose.yml

1 个答案:

答案 0 :(得分:0)

如何将socket.io作为依赖项添加到package.json?