【问题标题】:Heroku : Application Error When I try to load the page [Node.js + Express]Heroku:当我尝试加载页面时应用程序错误 [Node.js + Express]
【发布时间】:2021-06-22 22:13:30
【问题描述】:

index.js

// create an express app
const express = require("express");
const app = express();

// use the express-static middleware
app.use(express.static("public"));

// define the first route
app.get("/", function (req, res) {
  res.send("<h1>Hello World!</h1>");
});
  
// start the server listening for requests
app.listen(process.env.PORT || 3000, () => console.log("Server is running..."));


package.json

{
  "name": "SimpleApp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  },
  "engines": {
    "node": "10.x",
    "npm": "*"
  }
}

过程文件

web: echo "I don't want a web process"
service: npm start

我已经在heroku中部署了。

日志如下所示

2021-06-22T13:16:11.346614+00:00 app[service.1]: ------------------------------------------------------------------
2021-06-22T13:16:11.371025+00:00 app[service.1]: Server is running...

但是当我尝试加载页面时出现应用程序错误。

在本地运行良好

【问题讨论】:

    标签: node.js express heroku


    【解决方案1】:

    您的网络测功机可能默认关闭。您需要打开它才能运行您的 Heroku 应用

    【讨论】:

      【解决方案2】:

      在声明中间件时检查你设置的目录是否正确

      app.use(express.static("public"));
      

      如果不是,则使用 __dirname 像这样连接正确的目录

      const public = path.join(__dirname, "../public")

      然后

      app.use(express.static(public));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-16
        • 1970-01-01
        • 2021-02-17
        • 2019-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多