最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

为什么在代码在本地工作时,在heroku中使用node.js会给出不同的结果?

运维笔记admin18浏览0评论

为什么在代码在本地工作时,在heroku中使用node.js会给出不同的结果?

为什么在代码在本地工作时,在heroku中使用node.js会给出不同的结果?

我已将所有代码上传到Heroku。但是,我建议查看Heroku日志详细信息并在终端中收到此消息。

2019-02-12T13:23:48.102698+00:00 heroku[router]: at=error code=H10 
desc="App crashed" method=GET path="/favicon.ico" host=frozen- 
wildwood-17884.herokuapp request_id=55cdcdac-a94a-49f3-a050- 
920bad07f674 fwd="134.19.180.166" dyno= connect= service= status=503 
bytes= protocol=https

我还研究过其他页面,并试图在下面添加到我的server.js文件的底部,但没有运气。

app.listen(process.env.PORT || 3000, function(){
  console.log("Express server listening on port %d in %s mode", 
this.address().port, app.settings.env);
});

对此问题的任何建议将不胜感激。

const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const app = express()

const apiKey = '****************';

app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs')

app.get('/', function (req, res) {
  res.render('index', {weather: null, error: null});
})

app.post('/', function (req, res) {
  let city = req.body.city;
  let url = `.5/weather? 
q=${city}&units=metric&appid=${apiKey}`

request(url, function (err, response, body) {
  if(err){
  res.render('index', {weather: null, error: 'Error, please try 
again'});
} else {
  let weather = JSON.parse(body)
  if(weather.main == undefined){
    res.render('index', {weather: null, error: 'Error, please try again'});
  } else {
    let weatherText = `It's ${weather.main.temp} degrees in ${weather.name}!`;
    res.render('index', {weather: weatherText, error: null});
     }
   }
 });

})

结果显示在heroku应用程序的错误页面中。但是,该应用程序在本地运行完美。

回答如下:

从您的上一条注释和未定义process.env.PORT的情况看来,您似乎正在尝试在同一TCP端口上运行两个侦听器。这不起作用,因为侦听器将打开端口进行读取,导致所有其他尝试打开端口失败。

发布评论

评论列表(0)

  1. 暂无评论