Open Weather API Heroku 在本地系统上运行良好,但在 Heroku 上崩溃

Open Weather API Heroku worked fine on local system but crashes on Heroku

我正在使用开放天气 api。在我的本地系统上,我使用了这个请求并且工作正常。

"http://api.openweathermap.org/data/2.5/weather?lat="+lattitude+"&lon="+longitude+"&appid="+weatherKey+"&units="+unit"  

然后我在网上搜索了这个问题的解决方案,并将请求修改为:

https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/weather?lat="+lattitude+"&lon="+longitude+"&appid="+weatherKey+"&units="+unit

老实说,我不知道为什么我们要使用“https://cors-anywhere.herokuapp.com”。如果有人能解释一下意思,我会很高兴。 我仍在提前获得 error.Please 帮助 me.Thanks。 herku 日志

Server started on port 3000
2020-08-10T11:40:06.567155+00:00 heroku[web.1]: State changed from starting to up
2020-08-10T11:40:10.839569+00:00 heroku[router]: at=info method=GET path="/" host=news-with-tea.herokuapp.com request_id=03988840-7db9-47b6-a22d-5aa10d525a59 fwd="49.205.248.228" dyno=web.1 connect=1ms service=653ms status=304 bytes=184 protocol=https
2020-08-10T11:40:11.865181+00:00 heroku[router]: at=info method=GET path="/css/styles.css" host=news-with-tea.herokuapp.com request_id=e4b7fc2c-55b4-41f8-82fe-37eb7f2bb710 fwd="49.205.248.228" dyno=web.1 connect=0ms service=6ms status=200 bytes=2033 protocol=https
2020-08-10T11:40:11.931225+00:00 heroku[router]: at=info method=GET path="/weather.js" host=news-with-tea.herokuapp.com request_id=6c6b0987-9f84-4ea7-b32d-563c0cee6596 fwd="49.205.248.228" dyno=web.1 connect=0ms service=10ms status=200 bytes=3968 protocol=https
2020-08-10T11:40:12.265807+00:00 heroku[router]: at=info method=GET path="/weather.js" host=news-with-tea.herokuapp.com request_id=f08b5be2-1c62-4f69-9193-1e32494490ab fwd="49.205.248.228" dyno=web.1 connect=0ms service=2ms status=304 bytes=237 protocol=https
2020-08-10T11:40:27.640043+00:00 app[web.1]: helllo[object Object]
2020-08-10T11:40:29.742161+00:00 app[web.1]: 17 78
2020-08-10T11:40:30.245240+00:00 app[web.1]: undefined:1
2020-08-10T11:40:30.245258+00:00 app[web.1]: Missing required request header. Must specify one of: origin,x-requested-with
2020-08-10T11:40:30.245258+00:00 app[web.1]: ^
2020-08-10T11:40:30.245258+00:00 app[web.1]:
2020-08-10T11:40:30.245259+00:00 app[web.1]: SyntaxError: Unexpected token M in JSON at position 0
2020-08-10T11:40:30.245260+00:00 app[web.1]: at JSON.parse (<anonymous>)
2020-08-10T11:40:30.245260+00:00 app[web.1]: at IncomingMessage.<anonymous> (/app/app.js:137:29)
2020-08-10T11:40:30.245261+00:00 app[web.1]: at IncomingMessage.emit (events.js:315:20)
2020-08-10T11:40:30.245261+00:00 app[web.1]: at addChunk (_stream_readable.js:295:12)
2020-08-10T11:40:30.245262+00:00 app[web.1]: at readableAddChunk (_stream_readable.js:271:9)
2020-08-10T11:40:30.245262+00:00 app[web.1]: at IncomingMessage.Readable.push (_stream_readable.js:212:10)
2020-08-10T11:40:30.245263+00:00 app[web.1]: at HTTPParser.parserOnBody (_http_common.js:132:24)
2020-08-10T11:40:30.245263+00:00 app[web.1]: at TLSSocket.socketOnData (_http_client.js:469:22)
2020-08-10T11:40:30.245263+00:00 app[web.1]: at TLSSocket.emit (events.js:315:20)
2020-08-10T11:40:30.245264+00:00 app[web.1]: at addChunk (_stream_readable.js:295:12)
2020-08-10T11:40:30.252437+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/weather" host=news-with-tea.herokuapp.com request_id=c9e29b82-7b67-4af0-9746-a118f77c69e7 fwd="49.205.248.228" dyno=web.1 connect=0ms service=511ms status=503 bytes=0 protocol=https
2020-08-10T11:40:30.252876+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=POST path="/weather" host=news-with-tea.herokuapp.com request_id=a54e7548-a500-410a-8817-31e38dc20a2b fwd="49.205.248.228" dyno=web.1 connect=0ms service=2629ms status=503 bytes=0 protocol=https
2020-08-10T11:40:30.257162+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-08-10T11:40:30.257415+00:00 app[web.1]: npm ERR! errno 1
2020-08-10T11:40:30.259890+00:00 app[web.1]: npm ERR! ejs-challenge@1.0.0 start: `node app.js`
2020-08-10T11:40:30.260001+00:00 app[web.1]: npm ERR! Exit status 1
2020-08-10T11:40:30.260127+00:00 app[web.1]: npm ERR!
2020-08-10T11:40:30.260237+00:00 app[web.1]: npm ERR! Failed at the ejs-challenge@1.0.0 start script.
2020-08-10T11:40:30.260419+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-08-10T11:40:30.271263+00:00 app[web.1]:
2020-08-10T11:40:30.271644+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-08-10T11:40:30.271681+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-08-10T11_40_30_261Z-debug.log
2020-08-10T11:40:30.361346+00:00 heroku[web.1]: Process exited with status 1
2020-08-10T11:40:30.404906+00:00 heroku[web.1]: State changed from up to crashed
2020-08-10T11:45:24.731721+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=news-with-tea.herokuapp.com request_id=0b2f6496-cba4-40ea-94b1-d4de70aab61a fwd="49.205.248.228" dyno= connect= service= status=503 bytes= protocol=https
2020-08-10T11:45:24.969535+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=news-with-tea.herokuapp.com request_id=5f1db2b1-b242-4c8c-9541-4cf19dd486ed fwd="49.205.248.228" dyno= connect= service= status=503 bytes= protocol=https
2020-08-10T11:45:29.819254+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=news-with-tea.herokuapp.com request_id=3a5fed80-b456-452e-86bc-b47dd644f2b6 fwd="49.205.248.228" dyno= connect= service= status=503 bytes= protocol=https
2020-08-10T11:48:09.750615+00:00 heroku[web.1]: State changed from crashed to starting
2020-08-10T11:48:12.239984+00:00 heroku[web.1]: Starting process with command `npm start`
2020-08-10T11:48:14.253874+00:00 app[web.1]:
2020-08-10T11:48:14.253897+00:00 app[web.1]: > ejs-challenge@1.0.0 start /app
2020-08-10T11:48:14.253898+00:00 app[web.1]: > node app.js
2020-08-10T11:48:14.253898+00:00 app[web.1]:
2020-08-10T11:48:14.441205+00:00 app[web.1]: Server started on port 3000
2020-08-10T11:48:14.541655+00:00 heroku[web.1]: State changed from starting to up
app.get('/weather',function(req,res){
  const unit="metric";
  const url="https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/weather?lat="+lattitude+"&lon="+longitude+"&appid="+weatherKey+"&units="+unit;
  https.get(url,function(response){
    response.on("data",function(data){
     // convert data into JSON object
    const weatherData= JSON.parse(data);
    console.log(weatherData);
    const id=weatherData.weather[0].id;
    res.render('weather',{weatherData:weatherData,id:id});
    })
  })
 });

您不需要使用 "https://cors-anywhere.herokuapp.com/",API 不会区分您的本地请求或来自 heroku 服务器的请求。