简体   繁体   English

请求自己部署在Heroku上的Node.js

[英]Request myself in Nodejs deployed on Heroku

I have a Node Express App deployed on Heroku. 我在Heroku上部署了Node Express应用程序。 The main page needs around 50 HTTP Requests to external services to display the data. 主页需要大约50个对外部服务的HTTP请求才能显示数据。 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. 现在,我打算定期向我自己请求该网页,以强制Node继续刷新和缓存数据。 This way the user wont have to wait 1 minutes to see the page. 这样,用户将不必等待1分钟才能看到页面。

Following code works perfectly on local. 以下代码可在本地完美运行。 But at production enviroment (Heroku) I get Error: connect ECONNREFUSED 但是在生产环境(Heroku)下出现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? 有什么方法可以找到正确的IP /主机和端口吗?

UPDATE UPDATE

Name of the host ( https://nameofapp.herokuapp.com/ ) actually works. 主机名( https://nameofapp.herokuapp.com/ )实际上起作用。 But is there any way to call myself without knowing the host? 但是,有什么方法可以在不认识房东的情况下给自己打电话吗?

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 ) 设置环境变量BASE_URL是应用程序的URL, TIME_IDLING是请求超时(或者您可以在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 检查测功机的元数据 ,自动生成HEROKU_APP_NAME和一些envprocess.env.HEROKU_APP_NAME ),用它来获得你的主机

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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