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

知道吗?


当前回答

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

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

To:

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

其他回答

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

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

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

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

如果你得到这个错误,上面没有工作,错误不是来自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

编辑package.json:

...
"engines": {
"node": "5.0.0",
"npm": "4.6.1"
},
...

和Server.js:

...
var port = process.env.PORT || 3000;
app.listen(port, "0.0.0.0", function() {
console.log("Listening on Port 3000");
});
...

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