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