Reputation: 6762
I have a Node Express App deployed on Heroku. The main page needs around 50 HTTP Requests to external services to display the data. The requests take so long, so Im using cache.
Now Im tring to request the webpage periodically to myself in order to force Node to continue refreshing and caching the data. This way the user wont have to wait 1 minutes to see the page.
Following code works perfectly on local. But at production enviroment (Heroku) I get Error: connect ECONNREFUSED
setInterval(function() {
var url = "http://127.0.0.1:" + (process.env.PORT || 3000);
try {
http.get(url, {})
} catch (err) {
console.error('Error refreshing data')
console.error(err)
}
}, 1 * 60 * 1000)
I have tried:
http://127.0.0.1:" + (process.env.PORT || 3000)
https://127.0.0.1:" + (process.env.PORT || 3000)
0.0.0.0
Any way to find the correct ip/host and port?
UPDATE
Name of the host (https://nameofapp.herokuapp.com/
) actually works. But is there any way to call myself without knowing the host?
Upvotes: 0
Views: 174
Reputation: 4103
You can use request-myself
Try this code:
const requestMyself = require('request-myself');
const option = {
hostname: process.env.BASE_URL,
timeout: process.env.TIME_IDLING
};
app.use(requestMyself(option, (error, res) => {
if (error) {
console.error('RequestMyself error:', error);
} else {
console.info('RequestMyself statusCode:', res.statusCode);
}
}));
set your environment variable BASE_URL
is your app's url, TIME_IDLING
is timeout to request (or you can change when define option
)
If you has some problem, you feel free to create an issue
Check dyno-metadata, to auto generate HEROKU_APP_NAME
and some env
(process.env.HEROKU_APP_NAME
), use it to get your host
Upvotes: 1