Node.js 如何解决部署后heroku中的页面未找到错误

Node.js 如何解决部署后heroku中的页面未找到错误,node.js,express,heroku,Node.js,Express,Heroku,我正在尝试将express/node.js应用程序上载到heroku。应用程序已成功部署,但当我尝试访问url时,会出现错误:未找到 当我运行heroku日志--tail时,我得到: Error: ENOENT: no such file or directory, stat '/client/build/index.html' 所以我认为我在目录和statics文件夹上做错了什么 这是我的server.js文件: const express = require("express"); con

我正在尝试将express/node.js应用程序上载到heroku。应用程序已成功部署,但当我尝试访问url时,会出现错误:未找到

当我运行heroku日志--tail时,我得到:

Error: ENOENT: no such file or directory, stat '/client/build/index.html'
所以我认为我在目录和statics文件夹上做错了什么

这是我的server.js文件:

const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const passport = require("passport");
const cors = require("cors");
const path = require("path");

const users = require("./routes/api/users");
const profile = require("./routes/api/profile");
const matches = require("./routes/api/matches");

const app = express();

//body-parser middleware
app.use(bodyParser.urlencoded({ extended: false, limit: "50mb" }));
app.use(bodyParser.json({ limit: "50mb" }));

//db config
const db = require("./config/keys").mongoURI;

//cors
app.use(cors());

//connect to mongoose
mongoose
 .connect(db, { useNewUrlParser: true })
 .then(() => console.log("MongoDB connected"))
 .catch(err => console.log(err));

//Passport middleware
app.use(passport.initialize());

app.use("/images", express.static(path.join(__dirname + "/images")));

//Passport config
require("./config/passport")(passport);

//Use route
app.use("/api/users", users);
app.use("/api/profile", profile);
app.use("/api/matches", matches);

//Serve static assets if in production
if (process.env.NODE_ENV === "production") {
  app.enable("trust proxy");

  //Set static folder
  app.use(express.static(path.join(__dirname, "/../client/build")));

  app.get("*", (req, res) => {
    res.sendFile(path.join(__dirname + "/../client/build/index.html"));
  });
}

const port = process.env.PORT || 5000;

app.listen(port, () => console.log(`server running on port ${port}`));

我还包括一个文件夹“positions”的图像,我想应该是这样的

app.use("/images", express.static(path.join(__dirname, "images")));

编辑:实际上,您的应用程序希望在
/../client/build/index.html
中找到一个文件,但该文件不存在(这就是enoint error的意思)。因此,您需要创建预期的目录结构,或者配置应用程序,使其在index.html的正确目录中查找。这就是我现在所理解的,我希望这会对您有所帮助。

我通过在我的package.json中定义build和install命令解决了这个问题。Heroku会在那里寻找用于生产的构建命令

"scripts": {
    "build": "cd client && npm run build",
    "install": "cd client && npm install",
    "start": "node server",
    "server": "nodemon server",
    "client": "npm start --prefix client",
    "dev": "concurrently \"npm run server\" \"npm run client\""
  },

我也犯了类似的错误。对我来说,问题是我在package.json文件中有错误的build脚本,所以build根本没有创建

验证package.json文件中是否有如下所示的“heroku postbuild”脚本

"scripts": {
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm start --prefix client",
"clientinstall": "npm install --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client" 
},

我试过采纳你的建议,但问题仍然存在。我上传应用程序并获得未找到页面