首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在heroku上部署带有express的应用程序?

如何在heroku上部署带有express的应用程序?
EN

Stack Overflow用户
提问于 2022-02-09 01:35:19
回答 1查看 281关注 0票数 0

几天来,我一直试图在heroku上部署我的初学者应用程序,但没有成功。我跟踪了许多关于如何做的指南和答案,但是它没有起作用,下面是我的代码:

服务器

代码语言:javascript
复制
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 routes

package.json -服务器

代码语言:javascript
复制
{
  "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"
  }
}

应用程序代码

代码语言:javascript
复制
export const getPopular = () =>
  fetch(`/api/moviesPopular`)               
    --- more functions like this  

文件夹结构如下所示

代码语言:javascript
复制
root
├── server
│      
├── src
└── public

我在src文件夹中创建了setupProxy.js

代码语言:javascript
复制
const proxy = require("http-proxy-middleware");

module.exports = app => {
  app.use(proxy("/api/*", { target: "http://localhost:5000/" }));
};

package.json应用程序

代码语言:javascript
复制
{
  "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仪表板的日志里得到这个

代码语言:javascript
复制
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=https

api密钥位于heroku上的(Config Vars)中。

在我从应用程序的http://localhost:5000"中删除("proxy":“package.json”)后,该应用程序无法在本地工作

我能做些什么来部署在heroku上?在这样做之后,应用程序还能在本地工作吗?

EN

回答 1

Stack Overflow用户

发布于 2022-02-09 01:44:47

https://devcenter.heroku.com/articles/deploying-nodejs#specifying-a-start-script

文档提到端口被设置为一个环境变量。你在硬编码你的端口。

将侦听方法更改为:

代码语言:javascript
复制
app.listen(process.env.PORT || 5000, () => {
  console.log(`Server listening on ${app.address().port}`);
});

看看这会把你带到哪里。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71042984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档