几天来,我一直试图在heroku上部署我的初学者应用程序,但没有成功。我跟踪了许多关于如何做的指南和答案,但是它没有起作用,下面是我的代码:
服务器
const dotenv = require('dotenv')
dotenv.config()
const express = require("express");
//const PORT = 5000; got a binding error on heroku logs so i commented this
const app = express();
const path = require("path")
const fetch = require("node-fetch")
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
const cors = require ('cors') ;
app.use(cors());
if (process.env.NODE_ENV === 'production')
{app.use(express.static(path.resolve(__dirname, '../build')));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '../build', 'index.html'));
}); }
app.listen(5000, () => {
// console.log(`Server listening on ${PORT}`);
});
const url = "https://api.themoviedb.org/3/movie/"
const API_TMDB = process.env.API_TMDB
app.get('/api/moviesPopular' , async (req,res)=>{
--- // rest of code with similar get routespackage.json -服务器
{
"name": "server",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "node index.js" ,
"build": "cd .. && npm install && npm run build"
},
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^14.3.2",
"express": "^4.17.2",
"node-fetch": "2",
"nodemon": "^2.0.15"
},
"engines": {
"node": "14.15.0"
}
}应用程序代码
export const getPopular = () =>
fetch(`/api/moviesPopular`)
--- more functions like this 文件夹结构如下所示
root
├── server
│
├── src
└── public我在src文件夹中创建了setupProxy.js
const proxy = require("http-proxy-middleware");
module.exports = app => {
app.use(proxy("/api/*", { target: "http://localhost:5000/" }));
};package.json应用程序
{
"name": "movie",
"version": "0.1.0",
"private": true,
"dependencies": {
"@emotion/react": "^11.7.1",
"@emotion/styled": "^11.6.0",
"@mantine/core": "^3.6.2",
"@mantine/hooks": "^3.6.2",
"@mui/material": "^5.2.7",
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.5.0",
"baseui": "^10.7.1",
"bootstrap": "5.1.3",
"dotenv": "^10.0.0",
"global": "^4.4.0",
"gulp": "^4.0.2",
"http-proxy-middleware": "^2.0.3",
"react": "^17.0.2",
"react-bootstrap": "^2.1.0",
"react-dom": "^17.0.2",
"react-icons": "^4.3.1",
"react-router-dom": "^6.2.1",
"react-scripts": "5.0.0",
"react-slick": "^0.28.1",
"semantic-ui-css": "^2.4.1",
"semantic-ui-react": "^2.0.4",
"styletron-engine-atomic": "^1.4.8",
"styletron-react": "^6.0.2",
"web-vitals": "^2.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
} 我从heroku仪表板的日志里得到这个
2022-02-09T00:49:14.963015+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=moviedbx.herokuapp.com request_id=74d2e5a4-b7f1-45fc-bd3d-13914adcbb55 fwd="197.48.32.48" dyno= connect= service= status=503 bytes= protocol=https
2022-02-09T00:49:15.378141+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=moviedbx.herokuapp.com request_id=487a1c6a-850d-4e05-a9ff-6a325c4fa731 fwd="197.48.32.48" dyno= connect= service= status=503 bytes= protocol=httpsapi密钥位于heroku上的(Config Vars)中。
在我从应用程序的http://localhost:5000"中删除("proxy":“package.json”)后,该应用程序无法在本地工作
我能做些什么来部署在heroku上?在这样做之后,应用程序还能在本地工作吗?
发布于 2022-02-09 01:44:47
https://devcenter.heroku.com/articles/deploying-nodejs#specifying-a-start-script
文档提到端口被设置为一个环境变量。你在硬编码你的端口。
将侦听方法更改为:
app.listen(process.env.PORT || 5000, () => {
console.log(`Server listening on ${app.address().port}`);
});看看这会把你带到哪里。
https://stackoverflow.com/questions/71042984
复制相似问题