我正在使用 NodeJS 创建一个聊天应用程序,并且我想部署到 Heroku。但是,我收到错误:应用程序中发生错误,无法提供您的页面。如果您是应用程序所有者,请使用 Github 部署检查您的日志以获取详细信息。
有谁知道发生了什么事吗?
Here is some code to see what I have done.
{
"name": "chat",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./lib/index.js",
"test": "jasmine"
},
"dependencies": {
"express": "~4.13.1",
"firebase": "^3.9.0",
"request-promise": "^2.0.1",
"socket.io": "^1.4.5"
},
"devDependencies": {
"jasmine-sinon": "^0.4.0",
"jscs": "^2.11.0",
"proxyquire": "^1.7.4",
"rewire": "^2.5.1",
"sinon": "^1.17.3"
}
}
index.js (Server)
var express = require('express');
var app = express();
var path = require('path');
var http = require('http').Server(app);
var io = require('socket.io')(http);
var routes = require('./routes');
var chats = require('./chat');
app.use(express.static(path.join(__dirname, '../public')));
routes.load(app);
chats.load(io);
var port = process.env.PORT || 3000;
app.listen(port);
console.log('Server is listening at port:' + port);
最佳答案
我让该应用程序在 Heroku 中运行。您的代码中的问题是您没有为 heroku 设置正确的端口来工作。在您提供的代码中,您通过执行
设置正确的端口var port = process.env.PORT || 3000;
但是,在您的 GitHub 项目中,您将端口硬编码为 3000
http.listen(3000, function () {
console.log('listening on *:3000');
});
相反,您想要做的是允许 Heroku 定义端口或默认为 3000 以进行开发,如下所示..
var process = require('process');
var port = process.env.PORT || 3000;
http.listen(port, function() {
console.log("Server is listening on port: " + port);
});
完成后,部署到 Heroku,您应该会看到您的应用程序正在运行。
关于node.js - 如何将 NodeJS 应用程序部署到 Heroku?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44379802/