I have a mern stack app hosted on a hostinger vps. I am using pm2, express and nginx to serve the files. The app works fine except for the fact that i can not access files and images on my backend, that are located on subfolders of "uploads" folder. Whenever i try to access them, I get "500 (Internal Server Error)"
Here is my backend app.mjs with the exception of the routes.
const app = express();
app.use(bodyParser.json());
app.use("/uploads/images", express.static(path.join("uploads", "images")));
app.use((req, res, next) => {
//I Have this like this for testing purposes
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PATCH, DELETE");
next();
});
app.use((req, res, next) => {
const error = new HttpError("Could not find the route.", 404);
throw error;
});
const options = {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 5000, // 60 second timeout
useFindAndModify: false, //Adicionado por findone and update depcrecated.
};
mongoose
.connect(process.env.MONGODB_URI, options)
.then(() => {
app.listen(5000, "0.0.0.0", () => {
console.log("Estamos conectados!");
});
})
.catch((err) => {
console.log(err);
});
mongoose.connection.on("disconnected", () => {
console.log("MongoDB disconnected");
mongoose
.connect(process.env.MONGODB_URI, options)
.then(() => {
console.log("Estamos conectados!");
})
.catch((err) => {
console.log(err);
});
});
Here is my part of my nginx conf file without certbot and websocket
root /sites/dental/frontend/build;
index index.html;
server_name healthtechcv.com www.healthtechcv.com;
location / {
proxy_pass http://localhost:3000; # or which other port your app runs on
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /api{
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Host $host;
}
location /uploads{
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Host $host;
}
.....
I have tried granting full permission to the uploads file and it does not work. Sometimes I get an error saying "Could not find the route." Everything works fine locally.