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
Because You configure it to send index.html
backend/app.js
. Mismatches can lead to triggering the catch-all route./api/login
for a login API) align with backend routes.Set Content-Type Header or just clear client side cache by using
fetch
withcache: 'no-cache'