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

知道吗?


当前回答

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

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

其他回答

在开发应用程序时,我们需要以以下方式定义PORT:

const port = process.env.PORT || 4000; // PORT must be in caps

在将应用程序部署到服务器时,添加以下方法:

app.listen(port, () => {
 console.info("Server started listening.");
});

我们可以在本地运行主机名时将主机名作为第二个参数传递。但是在将其部署到服务器时,应该删除主机名参数。

app.listen(port, hostName, () => {
  console.info(`Server listening at http://${hostName}:${port}`);
});

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

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

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

当Heroku在服务器上绑定端口或主机名失败时发生错误。监听(port, [host], [backlog], [callback])。

Heroku需要的是.listen(process.env. port)或.listen(process.env. port)。港口,“0.0.0.0”)

所以更一般地,为了支持其他环境,使用这个:

var server_port = process.env.YOUR_PORT || process.env.PORT || 80;
var server_host = process.env.YOUR_HOST || '0.0.0.0';
server.listen(server_port, server_host, function() {
    console.log('Listening on port %d', server_port);
});

我浪费了一整天的时间思考后台有问题。在经历了这里所有的解决方案和彻底的日志检查后,意识到这是前端反应App.js有问题。

我换了每个

Axios.post(`${process.env.REACT_APP_BASE_URL}/createOrder`, {

})

to

Axios.post(`/createOrder`, {

})

它成功了!

显然,react文件与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。