I deployed my MERN stack app on Render. It’s a flight management app and when I go to the website on my computer, I’m able to add new flight information and it displays it on the homepage. When I open the site on another computer it also displays the information I added, but when I try to add new flight data, it won’t add it.
I originally thought it was an issue with MongoDB, so I allowed access from anywhere on it but that did not solve the problem.
This is my index.js:
const express = require('express');
const mongoose = require('mongoose');
require('dotenv').config();
const logger = require('./middleware/logger');
const cors = require('cors'); // Cross Origin Resource Sharing
console.log(process.env.MONGO_URI);
const app = express();
const PORT = process.env.PORT || 8080; // Default to 8080 if not in .env
app.use(express.json()); // This is middleware that auto parses JSON into JS objects between each HTTP request and the endpoint
app.use(cors()); // Allow all traffic
app.use(logger);
// router connections
app.use('/flights', require('./routes/flight.route'));
app.all('*', (req, res) => {
res.status(404).send('We don't have the resource you're looking for.');
});
// database connection
mongoose.connect(process.env.MONGO_URI)
.then(() => {
console.log('Successfully connected to MongoDB!');
})
.catch(err => {
console.error(err);
process.exit(1);
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}!`);
});
I wondered if the issue could be related to the React frontend package.json pointing to local host proxy:
{
"proxy": "http://localhost:8085"
}
I was wondering if I need to add the production api url to the .env but I’ve seen several tutorials where that wasn’t needed.
Dariel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
9