提问人:tddstuke 提问时间:10/30/2022 最后编辑:tddstuke 更新时间:11/20/2022 访问量:633
React 应用程序在部署到 heroku 时返回 HTTP 错误 404
React app returning HTTP error 404 when deployed to heroku
问:
我正在构建一个在本地运行良好的 React 应用程序,但是当我部署到 heroku 时,浏览器返回 404 not found 错误。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客户端定义了一个“/”路由:
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;
没有浏览器路由呈现,但所有 API 路由仍然有效。这似乎只是一个客户端问题。
我以为这一定是我server.js中的代码问题,但对我来说看起来是正确的。
server.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}`));
});
所以我想这可能是一个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"
},
我没有找到任何能够提供帮助的先前答案。似乎 heroku 在部署后找不到客户端构建,但我不明白为什么。我以前从未遇到过这个问题。
答:
1赞
tddstuke
11/20/2022
#1
溶液:
我以为我会在经过大量寻找和反复试验后发布对我有用的解决方案。所以就我而言,问题是我在 React JSX 中使用标签链接到其他 React Router 页面,而不是使用 react-router-dom 中的标签。我相信这是将请求发送到服务器端路由,而不是在客户端使用 React Router。我希望这能帮助其他人节省一些时间,他们可能会遇到同样的问题。<a>
<Link>
评论