简体   繁体   中英

What HTTP address can I send requests to from when hosting on Heroku? (client to server)

This sounds really dumb, but...

I have a Node.js Express server that used to be completely functioning on my localhost. I recently deployed it on Heroku and made some changes so my actual index.js listens to process.env.PORT instead of localhost. it listens to process.env.PORT fine through that, but I have a page rendered in React.js called app.js stored within a directory (./js/app.js) that makes HTTP requests when buttons are clicked.

However, whenever I try accessing process.env.PORT from within the app.js to make these requests, process is undefined, so everything falls apart. When working on it locally, I would just send the requests to http://localhost:3000/database , but now I have to change that since it's deployed on Heroku now.

How can I communicate between my client (app.js) and my server (index.js)?

I know this is something super simple and I've tried searching for it for the past couple of hours, but I just don't know how to phrase it.

Thank you!

Try to define your address and port in your app.js file as a heroku server address:

var server = app.listen(8000, function () {
var host = server.address().address;
var port = server.address().port; // put your adress here :)

console.log('Example app listening at http://%s:%s', host, port);
});

I am there if you get another pb :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM