我有我的第一个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/');

知道吗?


当前回答

我也遇到了同样的问题,我可以解决这个问题,将'localhost'替换为'0.0.0.0'的IP

其他回答

在所有我尝试过的解决方案中,没有人能像预期的那样工作,我研究了heroku默认情况下的。env文件应该保持约定的端口,进程。env。PORT, heroku默认情况下会寻找关键字PORT。

取消任何重命名,例如APP_PORT=,而是在env文件中使用PORT=。

我也有同样的问题,但是是express和apollo-server。解决方法:

唯一需要特别考虑的是允许 Heroku选择服务器部署到的端口。否则, 可能会出现错误,比如请求超时。 为了配置apollo-server在运行时使用Heroku定义的端口, 可以使用端口调用安装文件中的listen函数 由PORT环境变量定义:

> server.listen({ port: process.env.PORT || 4000 }).then(({ url }) => { 
> console.log(`Server ready at ${url}`); });

这也是一个好方法。为了解决这个错误

  const PORT = process.env.PORT || 3000                                                   
   app.listen(                                                                                 
    PORT,                                                                                                    
    '0.0.0.0',                                                                                
    function () {                                                                                                             
    console.log("Server started.......");                                                              
    }                                                                                                                   
  );

虽然这里的大多数答案都是有效的,但对我来说,问题是我运行了长进程作为npm run start的一部分,这导致了超时。

我在这里找到了解决方案,总结一下,我只需要将npm run build移动到postinstall任务。

换句话说,我改变了这个:

"start": "npm run build && node server.js"

:

"postinstall": "npm run build",
"start": "node server.js"

仔细想想,这完全是有道理的,因为随着我的应用不断发展,这种错误(以前偶尔出现)变得越来越普遍。

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

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

worker: YOUR_COMMAND

然后在CLI上运行:

heroku scale worker=1