简体   繁体   English

React 应用程序在部署到 heroku 时返回 HTTP 错误 404

[英]React app returning HTTP error 404 when deployed to heroku

I'm building a React app that works well locally but when I deploy to heroku the browser returns with a 404 not found error.我正在构建一个在本地运行良好的 React 应用程序,但是当我部署到 heroku 时,浏览器返回 404 not found 错误。 The heroku log shows that it's not finding anything to render at "/" heroku 日志显示它没有在“/”处找到要呈现的任何内容

2022-10-30T12:53:49.067388+00:00 heroku[router]: at=info method=GET path="/" host=agile-springs-04238.herokuapp.com request_id=63c0c0ba-dcc6-42f2-8242-e85fcb7ff6ae fwd="66.45.129.177" dyno=web.1 connect=0ms service=1ms status=404 bytes=178 protocol=https

APP.js in the client side has a "/" route defined though:客户端的 APP.js 定义了一个“/”路由:

import React, { useState } from "react";
import NavBar from "./components/Header";
import Home from "./pages/Home";
import SignUp from "./pages/SignUp";
import Login from "./pages/Login";
import SingleMovie from "./pages/SingleMovie";
import SingleShow from "./pages/SingleShow";
import Dashboard from "./pages/Dashboard";
import SearchBar from "./components/SearchBar";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

function App() {
  return (
    <Router>
      <NavBar />
      <SearchBar />
      <div className="">
        <Routes>
          <Route path="/" element={<Home />} />
          <Route path="/movieid/:id" element={<SingleMovie />} />
          <Route path="/showid/:id" element={<SingleShow />} />
          <Route path="/dashboard/:username" element={<Dashboard />} />
          <Route path="/signup" element={<SignUp />} />
          <Route path="/login" element={<Login />} />
        </Routes>
      </div>
    </Router>
  );
}

export default App;

No browser routes render but all API routes still work.没有浏览器路由呈现,但所有 API 路由仍然有效。 It seems to be a client side issue only.这似乎只是一个客户端问题。

I thought it must be a code problem in my server.js but it looks correct to me.我认为这一定是我的 server.js 中的代码问题,但对我来说它看起来是正确的。

server.js:服务器.js:

const express = require("express");
const routes = require("./controllers");
require("dotenv").config();
const PORT = process.env.PORT || 3001;
const path = require("path");
const sequelize = require("./config/connection");
const app = express();
const cors = require("cors");
const corsOptions = {
  origin: [
    "http://localhost:3000",
    "https://agile-springs-04238.herokuapp.com/",
  ],
  credentials: true, //access-control-allow-credentials:true
  optionSuccessStatus: 200,
};
app.use(cors(corsOptions));

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


app.use(routes);

if (process.env.NODE_ENV === "production") {
  app.use(express.static(path.join(__dirname, "../client/build")));
}

app.get("/*", (req, res) => {
  res.sendFile(path.join(__dirname, "../client/build/index.html"));
});

sequelize.sync({ force: false }).then(() => {
  app.listen(PORT, () => console.log(`Now Listening on ${PORT}`));
});

So I thought maybe it was a package.json script error.所以我想可能是 package.json 脚本错误。 But it appears that the build script is there as it should be.但看起来构建脚本应该在那里。

root package.json:根 package.json:

  "name": "naimdbv2",
  "version": "1.0.0",
  "main": "server/server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server/server.js",
    "dev": "concurrently \"cd server && npm run watch\" \"cd client && npm start\"",
    "install": "cd server && npm i && cd ../client && npm i",
    "seed": "cd server && npm run seed",
    "build": "cd client && npm run build",
    "watch": "webpack --watch --progress"
  },

I haven't found any previous answers that have been able to help.我还没有找到任何以前能够提供帮助的答案。 It seems like heroku is not finding the client build after being deployed but I can't understand why.似乎 heroku 在部署后找不到客户端构建,但我不明白为什么。 I've never run into this issue before.我以前从未遇到过这个问题。

Solution: Thought I'd post the solution that worked for me after a lot of looking and trial and error.解决方案:经过大量的查找和反复试验,我认为我会发布对我有用的解决方案。 So in my case the problem was I was using <a> tags in my React JSX to link to other React Router pages as opposed to using <Link> tags from react-router-dom.所以在我的例子中,问题是我在我的 React JSX 中使用<a>标签链接到其他 React Router 页面,而不是使用来自 react-router-dom 的<Link>标签。 I believe this was sending the request to the server side routes instead of using the React Router on the client side.我相信这是将请求发送到服务器端路由,而不是在客户端使用 React Router。 I hope this helps someone else save some time who may encounter the same issue.我希望这可以帮助其他可能遇到相同问题的人节省一些时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 刷新页面时出现404错误-部署在Heroku上的React App - 404 error when refreshing the page - React App deployed on Heroku React 应用程序在部署到 GCP 时出现 404 错误 - React app giving 404 error when deployed to GCP React 应用程序在本地工作,但部署到 heroku 时出现应用程序错误 - React app working locally but application error when deployed to heroku 部署反应和快递应用程序到 heroku 发送 404 未找到 - Deployed react and express app to heroku sends 404 not found 部署到Heroku时React应用程序崩溃 - React App Crashes When Deployed to Heroku 部署在 Heroku 上时,图像未加载到 React 应用程序中 - Images not loading in React app when deployed on Heroku Flask + React 应用程序在部署到 Heroku 时失败 - Flask + React App Fails When Deployed to Heroku 部署在 heroku 上的节点应用程序 - 出现 404 错误(即使 url 是正确的) - Node app deployed on heroku - getting 404 error (even when url is correct) 尝试登录部署在heroku上的应用程序时,auth0回调时出现404 - 404 on auth0 callback when trying to login on app deployed on heroku 部署到 Heroku 但在本地运行时,React 应用程序中的“.filter 不是函数”错误消息 - '.filter is not a function' error message in React app when deployed to Heroku but not when running locally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM