首页 > 解决方案 > 当我使用托管在 Heroku 上的 AWS 应用程序中的数据库部署我的反应节点时,“未找到”

问题描述

const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const port = process.env.PORT || 3002;

const cors = require("cors");
const path=require("path");
const routing  = require("./routing/route");
require('dotenv').config();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use("/",routing);

app.use(function (err, req, res, next) {
  console.log("hii")
  res.status(500).send({ message: err.message });
});

if(process.env.NODE_ENV ==="production"){

  app.use(express.static("client/build"));

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

app.listen(port, () => {
  console.log(` Server is started at http://localhost:${port}`);
});

我注意到我的后端在日志中运行我可以 Server is started at http://localhost:${port}从我的代码中看到 //console.log( )// 这部分。

但我的前端是这样工作的。

在日志文件中,我可以看到这些错误。

TypeError: path must be absolute or specified root to res.sendFile Error: ENOENT: no such file or directory, stat '/app/index.html'

我猜这两个错误都与同一路径有关。在此处输入图像描述

我也附上了我的文件夹结构

标签: javascriptnode.jsexpressheroku

解决方案


它与这个问题(已经有答案)非常相似- 我相信你可以在那里找到答案。

Express-js 无法获取我的静态文件,为什么?


推荐阅读