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

知道吗?


当前回答

在我的例子中,我使用了来自https://hapijs.com/的例子

来解决我替换的问题

server.connection({ 
    host: 'localhost', 
    port: 8000 
});

server.connection({
    port: process.env.PORT || 3000 
});

其他回答

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

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

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

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

:

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

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

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

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

不能为port设置一个固定的数字,heroku使用process.env.PORT动态分配它。但是你可以同时添加它们,就像这个process.env.PORT || 5000。Heroku将使用第一个,而您的本地主机将使用第二个。

您甚至可以添加回调函数。请看下面的代码

app.listen(process.env.PORT || 5000, function() {
    console.log("Server started.......");
});

Heroku动态地为你的应用程序分配端口,所以你不能将端口设置为一个固定的数字。Heroku将端口添加到env,所以你可以从那里拉它。让我们来听听这个:

.listen(process.env.PORT || 5000)

这样,当你在本地测试时,它仍然会监听端口5000,但它也可以在Heroku上工作。重要提示- PORT字必须大写。

你可以在这里查看Node.js上的Heroku文档。

我也有同样的问题,但我的环境变量设置得很好,npm和node的版本在package.json中指定。我发现这是因为,在我的情况下,Heroku需要在package.json中指定“start”:

  "scripts": {
    "start": "node index.js"
  }

把这个加到我的包里之后。我的节点应用程序成功部署在Heroku。