我有我的第一个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"

其他回答

我使用ReactJs, 如果你想上传到heroku,在你的webpack.config.js中添加这个

因为如果不加就会有错误

错误R10(引导超时)-> Web进程绑定$PORT失败 发射60秒

//webpack.config.js add code like that

const HtmlWebPackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
var server_port = process.env.YOUR_PORT || process.env.PORT || 5000;
var server_host = process.env.YOUR_HOST || "0.0.0.0";

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, "css-loader"]
      }
    ]
  },
  devServer: {
    disableHostCheck: true,
    contentBase: "./ dist",
    compress: true,
    inline: true,
    port: server_port,
    host: server_host

  },
  plugins: [
    new HtmlWebPackPlugin({
      template: "./src/index.html",
      filename: "index.html"
    }),
    new MiniCssExtractPlugin({
      filename: "[name].css",
      chunkFilename: "[id].css"
    })
  ]
};

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

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

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

在heroku中重新启动所有dynos对我来说是有用的

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

web: node app.js

要解决这个问题,请遵循以下四个简单步骤: 在包装里。json文件:

1-设置主字段为服务器文件:

"main": "server.js" // <-- here set you server file

2-在app.listen函数中添加host参数

const port = process.env.PORT || 3000;
const host = '0.0.0.0'
app.listen(port, host, ()=> connsole.log(`server is running on port ${port}`)

3-添加postinstall脚本到包。json文件

"scripts": {
     "postinstall": "npm run build", // <-- add this line
     "start": "node server.js" // <-- change server.js to you main file
}

4-在包中添加引擎字段。json文件

"engines": {
   "node": ">=14.0.O", // <-- change it to your node version. you can "node -v" in you command line
   "npm": ">=7.7.0" // <-- change this to your npm version. you can use "npm -v" in the command line to get your npm version
}

如果你成功了,请告诉我!