我有我的第一个node.js应用程序(本地运行良好)-但我无法通过heroku部署它(第一次w/ heroku也是如此)。代码如下。SO不让我写这么多代码,所以我只想说在我的网络中本地运行代码也没有问题。

 var http = require('http');
 var fs = require('fs');
 var path = require('path');

 http.createServer(function (request, response) {

    console.log('request starting for ');
    console.log(request);

    var filePath = '.' + request.url;
    if (filePath == './')
        filePath = './index.html';

    console.log(filePath);
    var extname = path.extname(filePath);
    var contentType = 'text/html';
    switch (extname) {
        case '.js':
            contentType = 'text/javascript';
            break;
        case '.css':
            contentType = 'text/css';
            break;
    }

    path.exists(filePath, function(exists) {

        if (exists) {
            fs.readFile(filePath, function(error, content) {
                if (error) {
                    response.writeHead(500);
                    response.end();
                }
                else {
                    response.writeHead(200, { 'Content-Type': contentType });
                    response.end(content, 'utf-8');
                }
            });
        }
        else {
            response.writeHead(404);
            response.end();
        }
    });

 }).listen(5000);

 console.log('Server running at http://127.0.0.1:5000/');

知道吗?


当前回答

我会建议你很好地阅读日志,以便轻松排除故障。 如果你引用一个PORT作为环境变量(env),请确保它是PORT而不是PORT,因为heroku会在你部署应用程序时自动分配一个PORT号。process.env.PORT不是process.env.PORT

其他回答

值得一提的是,如果你的代码没有指定端口,那么它就不应该是一个web进程,而应该是一个工作进程。

因此,将你的Procfile更改为read(填入你的特定命令):

worker: YOUR_COMMAND

然后在CLI上运行:

heroku scale worker=1

我会建议你很好地阅读日志,以便轻松排除故障。 如果你引用一个PORT作为环境变量(env),请确保它是PORT而不是PORT,因为heroku会在你部署应用程序时自动分配一个PORT号。process.env.PORT不是process.env.PORT

就我而言,我有两个问题……

1)没有监听器,因为从另一个入口文件运行应用程序,这个运行脚本从包中删除。json“脚本”

2)用“Sequelize”而不是“Sequelize”区分大小写的问题

在我的例子中,端口和主机都不是问题。index.js被分成了两个文件。server.js:

//server.js
const express = require('express')
const path = require('path')

const app = express()

app.use(express.static(path.resolve(__dirname, 'public')));
// and all the other stuff
module.exports = app

//app.js
const app = require('./server');
const port = process.env.PORT || 3000;
app.listen(port, '0.0.0.0', () => {
    console.log('Server is running s on port: ' + port)
});

从包中。我们运行node app。js。

显然这就是问题所在。一旦我将两者合并到一个文件中,Heroku应用程序就会按预期部署。

我的情况是,我在启动时运行数据库脚本,花费了很长时间。我在部署完成后手动运行npm start来解决这个问题。