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

知道吗?


当前回答

如果你得到这个错误,上面没有工作,错误不是来自R10本身。错误是由R14(内存配额超过)引起的,这是因为部分代码使用multer (multi . diskstorage()方法)或其他一些库来存储使用某种形式的本地存储的图像或其他文件。R14错误导致的延迟将导致R10(引导超时)错误。

解决方案:

在你的包裹里。Json文件,把你的开始改为“start”:“node——max-old-space-size=4096 server/index.js”。这增加了存储大小。

also

设置这个heroku变量 heroku canfig:set NODE_ENV=dev

其他回答

我有一个类似的问题,试图解决它几个小时(H10 GET=/)。我尝试改变PORT,但没有结果。事实证明,解决方法很简单。我修改了包里的一行。json文件。从nodemon到node,成功了:

"scripts": {
"start": "nodemon app.js"

To:

 "scripts": {
 "start": "node app.js"

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

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

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

将监听端口从3000更改为(process.env.)。PORT || 5000)解决。

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

我遇到了同样的问题,因为我没有定义Procfile。提交一个文本文件到应用程序的根目录Procfile,不带文件扩展名。这个文件告诉Heroku运行哪个命令来启动应用程序。

web: node app.js