I created an app on Heroku, and pulled my GitHub repository.
My app displays well, but the port of my backend change every time I have a new build.
Here is how I tried to set it :
const PORT = process.env.PORT || 5000
const corsOptions = {
origin: 'localhost',
credentials: true,
optionsSuccessStatus: 200
}
app.use(cors(corsOptions));
app.use(express.json());
app.use(express.urlencoded({
extended: true
}))
app.use(cookieParser())
if (process.env.NODE_ENV === "production") {
app.use(express.static('client/build'))
}
// routes
app.use('/api', routes)
//server
app.listen(PORT, () => {
console.log(`Listening on port ${process.env.PORT}`)
})
Even if I set only 5000
, I have a port like Listening on port 54435
Any idea how to have a fixed port for my backend ?
I need it because my axios request with my frontend (React) need the port (obviously)
After deploying your app to heroku, heroku gives you a link e.g. mynewapp.heroku.com
.
Now in your axios request of frontend (React), replace all the
http://localhost:5000
with mynewapp.heroku.com
.
Here no need to specify any port number.