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

知道吗?


当前回答

在我的例子中,端口和主机都不是问题。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应用程序就会按预期部署。

其他回答

从heroku bash进程中,使用选项解析器(如yargs)将$PORT的值传递给节点应用程序。

下面是一个示例,说明如何做到这一点。在脚本对象上,在包内。添加一个启动方法"node server——port $ port "。

在你的服务器文件中,使用yargs从start方法的port选项(——port $ port)中获取值:

const argv = require('yargs').argv;
const app = require('express')();

const port = argv.port || 8081;

app.listen(argv.port, ()=>{
    console.log('Probably listening to heroku $PORT now ', argv.port); // unless $PORT is undefined, in which case you're listening to 8081.
});

现在当你的应用程序启动时,它将绑定到动态设置的值$PORT。

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

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

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

对我来说,问题在于资本化。你知道,在数小时的编码之后,你的眼睛会错过一些小细节。

在我的代码中,我使用process.env.port而不是process.env。PORT,所以要注意,PORT环境变量必须是大写的。

请看下面的例子:

const PORT = process.env.PORT || 3000;
const HOST = process.env.HOST || '0.0.0.0';

const express = require('express');
const app = express();

app.listen(PORT, HOST, () => {
      console.log('Server started on ' + HOST + ':' + PORT);
})

在我的例子中,我使用Babel plugin-transform-inline-environment-variables插件。显然,Heroku在进行部署时没有设置PORT env变量,因此process.env.PORT将被undefined取代,并且您的代码将回退到Heroku不知道的开发端口。

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

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