简体   繁体   中英

“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}`);
});

What I have noticed that my backend is running as in log I can see the //console.log( Server is started at http://localhost:${port} )// this part from my code.

But my frontend is that working.

In the log file I can see these error.

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

I guess both the error are related to the same path. enter image description here

I have attached my folder structure also

It's very similar to this question (which already has answer) - I sure you can find answer there.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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