NodeJS Heroku API 在连接到 React build 时不断抛出“Web 进程无法在启动后 60 秒内绑定到 $PORT”

编程入门 行业动态 更新时间:2024-10-09 05:22:48

NodeJS Heroku API 在连接到 React build 时不断抛出“Web 进程无法在启动后 60 秒内<a href=https://www.elefans.com/category/jswz/34/1769902.html style=绑定到 $PORT”"/>

NodeJS Heroku API 在连接到 React build 时不断抛出“Web 进程无法在启动后 60 秒内绑定到 $PORT”

我已成功将 node.js API 部署到 heroku,并且能够从位于 http://localhost:8000/ 的本地服务器运行 React 生产 buid。但是,当我点击部署的端点“https://-api.herokuapp/categories”和“https://-api.herokuapp/services”时,我取回 json 有效负载而不是我的客户端应用程序。此外,当我点击“https://************-api.herokuapp”时,返回“Cannot GET /”。不确定我做错了什么,但我确实注意到,我也无法从我的前端构建 url 访问我的本地 api 端点(只能从我的本地服务器)。我相信这两个问题是相关的。在过去的几天里,我一直在尝试使用来自 stack overflow、youtube 和在线开发博客的建议来解决这个问题。什么都不管用。任何正确方向的建议或轻推都表示赞赏。

后端目录结构

testanywhere-rebuild
  --backend
    --index.js
  --app.js

客户端目录结构

 testanywhere-rebuild-frontend
  --build
    --index.html

简介

web: node app.js

index.js(后端)

import express from "express"
import connectToDB from "./config/db.js"
import appointmentRouter from "./routes/appointmentRoutes.js"
import userRouter from "./routes/userRoutes.js"
import serviceRouter from "./routes/serviceRoutes.js"
import categoryRouter from "./routes/categoryRoutes.js"
import { fileURLToPath } from 'url';
import path from "path"
import * as dotenv from 'dotenv' 
dotenv.config()

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const app = express()
connectToDB()
app.use(express.json());

app.use("/appointments", appointmentRouter)
app.use("/users", userRouter)
app.use("/services", serviceRouter)
app.use("/categories", categoryRouter)

app.use(express.urlencoded({ extended: false }));

// Serve frontend
if (process.env.NODE_ENV === 'production') {
    app.use(express.static(path.join(__dirname, '../../testanywhere-rebuild-frontend/build')));
  } else {
    app.get('/', (req, res) => res.send('Please set to production'));
  }

export default app

app.js(后端)

import app from "./backend/index.js"
import * as dotenv from 'dotenv' 
dotenv.config()
const port = process.env.PORT || 3000
app.listen(port, () => {
    console.log(`Listening on port ${port}`)
})

package.json

{
  "name": "testanywhere-rebuild",
  "version": "1.0.0",
  "description": "API for Testanywhere",
  "engines": {
    "node": "16.16.0",
    "npm": "8.11.0"
  },
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "jest --watch --detectOpenHandles --force-exit",
    "start": "node backend/index.js",
    "server": "nodemon app.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^5.1.0",
    "bcryptjs": "^2.4.3",
    "colors": "^1.4.0",
    "crypto": "^1.0.1",
    "dotenv": "^16.0.3",
    "email-validator": "^2.0.4",
    "express": "^4.18.2",
    "express-async-handler": "^1.2.0",
    "googleapis": "^113.0.0",
    "jest": "^29.5.0",
    "jsonwebtoken": "^9.0.0",
    "mongoose": "^6.9.2",
    "nodemailer": "6.9.1",
    "password-validator": "^5.3.0",
    "supertest": "^6.3.3",
    "uuidv4": "^6.2.13"
  },
  "devDependencies": {
    "@shelf/jest-mongodb": "^4.1.7",
    "concurrently": "^7.6.0",
    "nodemon": "^2.0.20"
  }
}
回答如下:

更多推荐

NodeJS Heroku API 在连接到 React build 时不断抛出“Web 进程无法在启动后 60 秒内绑定到 $PORT”

本文发布于:2024-05-13 15:43:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1759913.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   连接到   抛出   进程   NodeJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!