【问题标题】:"Not found" when I am deploying my react node with database hosted in AWS application on heroku当我使用托管在 Heroku 上的 AWS 应用程序中的数据库部署我的反应节点时,“未找到”
【发布时间】:2020-08-20 12:25:35
【问题描述】:
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}`);
});

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

但我的前端可以正常工作。

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

TypeError: path must be absolute or指定root to res.sendFile 错误:ENOENT:没有这样的文件或目录,stat '/app/index.html'

我猜这两个错误都与同一个路径有关。enter image description here

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

【问题讨论】:

  • 我已经尝试过 path.join,因为我在其他地方看到过它,但在这种情况下我遇到了内部服务器错误。

标签: javascript node.js express heroku


【解决方案1】:

这与question(已经有答案)非常相似——我相信你可以在那里找到答案。

Express-js can't GET my static files, why?

【讨论】:

  • 如何将以下解决方案应用于我的问题?我在 app.use(express.static("client/build")); 中做错了吗?
  • 其实Heroku git仓库有问题,我的客户端文件夹是空的,所以静态函数无法识别路径,因此显示未找到。
猜你喜欢
  • 2019-11-24
  • 2021-09-12
  • 2017-01-07
  • 2021-04-25
  • 2018-07-06
  • 1970-01-01
  • 2018-11-20
  • 2022-01-16
  • 2016-10-10
相关资源
最近更新 更多