skip to Main Content

during the production all the api’s are fetched right but i am getting the response as index.html file instead of json data as you can see that i am getting index.html as response while during development i used to get json data from my backend

here is my code

server.js


const app = require("./backend/app.js");
const path = require('path');
const express = require("express");
const dotenv = require('dotenv');
const connectDatabase = require("./backend/config/database.js");
const cloudinary = require('cloudinary');
dotenv.config({path : "/Users/kapil/Documents/Ecommerce/backend/config/.env"});
const PORT = process.env.PORT || 4000;

// UncaughtException Error
process.on('uncaughtException', (err) => {
console.log(`Error: ${err.message}`);
process.exit(1);
});

connectDatabase();

// deployment
__dirname = path.resolve();
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, './frontend/build')))

    app.get('/*', (req, res) => {
        res.sendFile(path.resolve(__dirname, './frontend/build/index.html'))
    });

} else {
app.get('/', (req, res) => {
res.send('Server is Running!');
});
}

cloudinary.config({
cloud_name: process.env.CLOUDINARY_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});

app.listen(PORT , () => {
console.log(`server working on http://localhost:${PORT}`);
})

// Unhandled Promise Rejection
process.on('unhandledRejection', (err) => {
console.log(`Error: ${err.message}`);
server.close(() =

process.exit(1);
});
});

during development phase it works well and yes there was a problem in the reducers file before due to that problem i was able to see just the white screen and now when i routing to /login again than problem prevails

2

Answers


  1. Because You configure it to send index.html

    enter image description here

    Login or Signup to reply.
    • Double-check that your frontend code is making requests to the correct API endpoints defined in backend/app.js. Mismatches can lead to triggering the catch-all route.
    • Ensure your frontend routes (e.g., /api/login for a login API) align with backend routes.

    Set Content-Type Header or just clear client side cache by using fetch with cache: 'no-cache'

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search