Reputation: 913
I've got an express app with a nextJS frontend both deploy together on Heroku. Due to Heroku's dynamic port assignment, I'm trying to pass the PORT into the static nextJS build so that it knows how to call the API i.e
const PORT = process.env.PORT;
fetch(`https://localhost:${PORT}/api/blah`)
I've tried the following in my package.json but it doesn't seem to work:
"scripts": {
"test": "jest",
"dev": "nodemon",
"build": "next build && tsc --project tsconfig.server.json",
"start": "cross-env NODE_ENV=production node dist/index.js -p $PORT"
},
The port is accessible within the NODE application but not within the client repo.
Upvotes: 0
Views: 577
Reputation: 1229
Except for your package.json
, you don't need to use ${PORT}
in all other frontend files.
Use fetch('/api/user/2024')
instead of fetch ('http://localhost:3000/api/user/2024')
.
If you stumble upon any absolute URL error
during heroku build(can be seen during build process i.e build log). Use fetch('http://appname.herokuapp.com/api/user/2024')
instead of fetch('/api/user/2024')
and replace appname
with your heroku appname
.
Upvotes: 0