【发布时间】:2022-01-15 22:00:05
【问题描述】:
这是我第一次在 heroku 上部署,我不断收到错误,构建成功但是当构建完成并且我尝试通过 heroku 在浏览器中查看 API 响应时,我得到一个错误 -
无法加载资源:服务器响应状态为 500(内部服务器错误)
来自 heroku 的日志 -
2021-12-11T19:16:54.702260+00:00 应用 [web.1]:npm 错误!错误号 1
2021-12-11T19:16:54.708322+00:00 应用 [web.1]:npm 错误! example-create-react-app-express@1.0.0 开始:node server.js
2021-12-11T19:16:54.708419+00:00 应用 [web.1]:npm 错误!退出状态 1
2021-12-11T19:16:54.708524+00:00 应用 [web.1]: npm 错误!
2021-12-11T19:16:54.708602+00:00 应用 [web.1]:npm 错误!在 example-create-react-app-express@1.0.0 启动脚本中失败。
2021-12-11T19:16:54.708721+00:00 应用 [web.1]:npm 错误!这可能不是 npm 的问题。上面可能还有额外的日志输出。
2021-12-11T19:16:54.718471+00:00 应用 [web.1]:
2021-12-11T19:16:54.719294+00:00 应用 [web.1]:npm 错误!可以在以下位置找到此运行的完整日志:
2021-12-11T19:16:54.719298+00:00 应用程序 [web.1]:npm 错误! /app/.npm/_logs/2021-12-11T19_16_54_709Z-debug.log
2021-12-11T19:16:54.908600+00:00 heroku[web.1]:进程以状态 1 退出
2021-12-11T19:16:55.067169+00:00 heroku[web.1]:状态从开始变为崩溃
2021-12-11T19:16:56.373561+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/api/hello" host=agile-spire-07157.herokuapp .com request_id=ab0c71fb-1be1-4829-b123-e57cc4e7fafd fwd="67.177.196.118" dyno= connect= service= status=503 bytes= protocol=https
2021-12-11T19:17:56.820732+00:00 heroku[路由器]: at=error code=H10 desc="App crashed" method=GET path="/" host=agile-spire-07157.herokuapp.com request_id =b9615ee7-190d-44ce-8d97-c099d26149ca fwd="67.177.196.118" dyno= connect= service= status=503 bytes= protocol=https
非常感谢任何帮助
server.js:
const express = require('express');
const axios = require('axios');
require('dotenv').config();
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
app.use(cors());
app.use(express.static(path.join(__dirname, 'client/build')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'));
});
const port = process.env.PORT || 2000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const apiKey = process.env.APIKEY
app.get('/api/hello', (req, res) => {
const { term, location } = req.query
const config = {
method: 'get',
url: `https://api.yelp.com/v3/businesses/search?term=${term}&location=${location}`,
headers: {
'Authorization': `Bearer ${apiKey}`
}
};
axios(config)
.then(function (response) {
return JSON.stringify(response.data, null, 2)
})
.then(function (jsonResponse) {
res.send(jsonResponse)
})
.catch(function (error) {
console.log(error);
});
});
app.listen(port, () => console.log(`Listening on port ${port}`))
服务器的package.json:
{
"name": "example-create-react-app-express",
"version": "1.0.0",
"scripts": {
"start": "node server.js",
"client": "cd ../client && yarn start",
"server": "nodemon server.js",
"dev": "concurrently --kill-others-on-fail \"yarn server\" \"yarn client\""
},
"engines": {
"node": "14.17.3"
},
"dependencies": {
"axios": "^0.24.0",
"body-parser": "^1.18.3",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.1"
},
"devDependencies": {
"concurrently": "^4.0.1"
}
}
【问题讨论】:
标签: node.js reactjs express heroku