繁体   English   中英

当我使用托管在 heroku 上的 AWS 应用程序中的数据库部署我的反应节点时,“未找到”

[英]“Not found” when I am deploying my react node with database hosted in AWS application on heroku

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 specified root to res.sendFile Error: ENOENT: no such file or directory, stat '/app/index.html'

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM