Node.js 503部署到Heroku后,单个路由出现错误,其他路由正常工作

Node.js 503部署到Heroku后,单个路由出现错误,其他路由正常工作,node.js,reactjs,heroku,Node.js,Reactjs,Heroku,我正在使用React和Node构建一个完整的堆栈应用程序,尽管在本地工作,但在部署到Heroku后,我的一个API路由给了我一个503错误。不管出于什么原因,我的所有其他路线在部署后都可以正常工作。在上下文中,这曾经是我正在进行的一个小组项目,但我决定进行重构,以便它能更好地工作。我自己对服务器做了很少的更改,而且在我们之前部署时,它工作得很好(问题更多地出现在React端) 我不完全确定我的代码在哪里出错了,所以这里是github: 这是我的app.js文件。不起作用的路由是/api/pupp

我正在使用React和Node构建一个完整的堆栈应用程序,尽管在本地工作,但在部署到Heroku后,我的一个API路由给了我一个503错误。不管出于什么原因,我的所有其他路线在部署后都可以正常工作。在上下文中,这曾经是我正在进行的一个小组项目,但我决定进行重构,以便它能更好地工作。我自己对服务器做了很少的更改,而且在我们之前部署时,它工作得很好(问题更多地出现在React端)

我不完全确定我的代码在哪里出错了,所以这里是github:

这是我的app.js文件。不起作用的路由是/api/puppyfinder

// Import dependencies
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
const path = require('path');
const bodyParser = require('body-parser');
require('dotenv').config();

// Middleware stuffs
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

app.use(express.static('build'));

//API route
app.use('/api/puppy', require('./routes/puppy-routes'));
app.use('/api/opinions', require('./routes/opinion-routes'));
app.use('/api/puppyfinder', require('./routes/puppy-finder-routes'));
app.get('/*', (req,res) => {
  res.sendFile(path.join(__dirname + '/index.html'))
});

// Setting up the port
app.listen(PORT, () => {
  console.log(`Live on port ${PORT} but also it's the Shrek movie`);
});
这是我试图调用的React组件中的方法

// changes the redirect state so the component knows to redirect to the results page

handleSubmit(event) {
    event.preventDefault();
    // Post to the puppyfinder controller, which will make a get request to the puppy finder API
    // Sends the breed and zipcode to the controller
    axios({
      method: 'POST',
      url: '/api/puppyfinder',
      data: {
        breed: this.state.breed,
        zipcode: this.state.zipcode,
      },
    })
    // Checks to see if puppy data was returned by putting it into an array
    .then(puppyData => {
      console.log(puppyData.data.data.pet);
      if (puppyData.data.data.pet.length) {
        this.setState({
          puppiesLoaded: true,
          puppyData: puppyData.data.data.pet,
        });
      }
      else {
        alert(`Sorry, but we couldn't find any ${this.state.breed} dogs nearby ${this.state.zipcode}. Maybe try something else?`)
      }
    })
    .catch(err => {
      console.log('puppyfinder call error', err);
    })
  }
编辑:我一直在更仔细地查看heroku日志,下面是我尝试发布到服务器时得到的信息。这让我觉得问题可能出在我的控制器上,但当我作为一个组部署时,它工作得很好,从那时起我就没有更改过该文件。还在当地工作

2018-04-03T16:43:30.656152+00:00 app[web.1]: inside puppy finder API call error TypeError: Cannot read property 'pets' of undefined
2018-04-03T16:43:30.656164+00:00 app[web.1]:     at axios.then.allTheDogs (/app/controllers/puppy-finder-controller.js:12:43)
2018-04-03T16:43:30.656169+00:00 app[web.1]:     at process._tickCallback (internal/process/next_tick.js:118:7)
2018-04-03T16:43:30.656166+00:00 app[web.1]:     at <anonymous>
2018-04-03T16:44:00.622922+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/api/puppyfinder" host=fathomless-bayou-33817.herokuapp.com request_id=b008d8bd-750e-4681-89e5-7fcf1999dcfe fwd="100.12.160.42" dyno=web.1 connect=1ms service=30001ms status=503 bytes=0 protocol=https
2018-04-03T16:43:30.656152+00:00应用程序[web.1]:内部puppy finder API调用错误类型错误:无法读取未定义的属性“pets”
2018-04-03T16:43:30.656164+00:00应用程序[web.1]:在axios.then.allTheDogs(/app/controllers/puppy finder controller.js:12:43)
2018-04-03T16:43:30.656169+00:00应用程序[web.1]:在进程中进行回调(内部/进程/下一个进程js:118:7)
2018-04-03T16:43:30.656166+00:00应用程序[网站1]:在
2018-04-03T16:44:00.622922+00:00 heroku[路由器]:at=error code=H12 desc=“请求超时”method=POST path=“/api/puppyfinder”主机=深不可测的bayou-33817.herokuapp.com请求id=b008d8bd-750e-4681-89e5-7fcf1999dcfe fwd=“100.12.160.42”dyno=web.1连接=1ms服务=30001ms状态=503字节=0协议=https

解决了这个问题,没有为我正在使用的第三方API设置我的私有API密钥。是在我的.env文件中本地设置的,但不是在heroku上。为便于将来参考,请使用
$heroku config:set VARIABLE=value

设置您放入.env文件中的任何内容,查看您的heroku服务器日志。我收到的错误消息是:at=error code=H12 desc=“Request timeout”method=POST path=“/api/puppyfinder”host=fethomless-bayou-33817.herokuapp.com请求_id=b008d8bd-750e-4681-89e5-7fcf1999dcfe fwd=“100.12.160.42”dyno=web.1 connect=1ms service=30001ms status=503 bytes=0 protocol=httpsI我将编辑帖子,并提供有关heroku日志的更多详细信息。