React 路由与 Express 路由发生冲突。

huangapple go评论103阅读模式
英文:

React routes gets conflicted with express routes

问题

问题是当您访问 http://localhost:5000/api/users 时,您从您的React应用程序收到了404未找到错误。要解决这个问题,您需要确保以下几点:

  1. Express路由配置:确保您的Express应用程序在处理 http://localhost:5000/api/users 请求时正确配置路由。您已经有一个路由处理 /api/users,但是它仅返回一个JSON响应,这是正确的。但是,如果您想在浏览器中访问此URL并在React应用程序中渲染页面,您需要创建一个用于渲染React页面的路由。

  2. React Router配置:在React应用程序中,确保您正确配置了React Router,以便它可以处理 http://localhost:5000/api/users 请求并渲染相应的组件。您的React Router配置看起来已经处理了根路径 //login/register,但如果您想处理 /api/users,您需要添加一个适当的Route。

如果您需要更详细的指导,请提供您的Express和React Router配置,以便我可以为您提供更具体的帮助。

英文:

I have the build files (from npm run build) of the react application in the /public folder on my backend folder.

This is what is in my index.js on my backend

  1. const express = require('express')
  2. const mysql = require('mysql');
  3. const path = require('path')
  4. const app = express()
  5. app.use(express.static(path.join(__dirname + "/public")))
  6. app.use(express.json())
  7. const PORT = 5000
  8. app.listen(PORT)
  9. app.get('/*', function(req, res) {
  10. res.sendFile(path.join(__dirname, '/public/index.html'), function(err) {
  11. if (err) {
  12. res.status(500).send(err)
  13. }
  14. })
  15. })
  16. const db = mysql.createConnection({
  17. user: "root",
  18. host: "localhost",
  19. password: "",
  20. database: "project_db",
  21. });
  22. const users =
  23. [
  24. { id: 1,name: "Andrew" },
  25. { id: 2, name: "Steve" },
  26. { id: 3, name: "John" },
  27. { id: 4, name: "Michael" },
  28. { id: 5, name: "Christian" }
  29. ];
  30. app.get("/api/users", (req, res) => {
  31. res.json(users);
  32. })
  33. app.post("/api/register", (req, res) => {
  34. const username = req.body.username;
  35. const password = req.body.password;
  36. const email = req.body.email;
  37. db.query("INSERT INTO users (name, password, email) VALUES (?, ?, ?)", [username, password, email]);
  38. res.send({
  39. "message": "account registered",
  40. "status": 1,
  41. });
  42. });

App.js on my react app

  1. import { Route, Routes } from 'react-router-dom'
  2. import './components/App.css'
  3. import Home from './pages/Home'
  4. import Login from './pages/Login'
  5. import Register from './pages/Register'
  6. function App() {
  7. return (
  8. <>
  9. <Routes>
  10. <Route path="/" element={<Home />} />
  11. <Route path="/login" element={<Login />} />
  12. <Route path="/register" element={<Register />} />
  13. <Route path="*" element={ <h1>Not found (404)</h1> } />
  14. </Routes>
  15. </>
  16. );
  17. }
  18. export default App;

My question: When I go to http://localhost:5000/api/users it gets the 404 not found from my react app

How can I fix this problem?

答案1

得分: 1

以下是您要翻译的部分:

List your backend API endpoints before the "*" "endpoint" that returns the React app.

// Backend APIs
app.get("/api/users", (req, res) => {
res.json(users);
})

app.post("/api/register", (req, res) => {
const username = req.body.username;
const password = req.body.password;
const email = req.body.email;

db.query("INSERT INTO users (name, password, email) VALUES (?, ?, ?)", [username, password, email]);

res.send({
"message": "account registered",
"status": 1,
});
});

// Frontend React app routes
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, '/public/index.html'), function(err) {
if (err) {
res.status(500).send(err)
}
})
});

英文:

List your backend API endpoints before the "*" "endpoint" that returns the React app.

  1. const express = require('express');
  2. ...
  3. // Backend APIs
  4. app.get("/api/users", (req, res) => {
  5. res.json(users);
  6. })
  7. app.post("/api/register", (req, res) => {
  8. const username = req.body.username;
  9. const password = req.body.password;
  10. const email = req.body.email;
  11. db.query("INSERT INTO users (name, password, email) VALUES (?, ?, ?)", [username, password, email]);
  12. res.send({
  13. "message": "account registered",
  14. "status": 1,
  15. });
  16. });
  17. // Frontend React app routes
  18. app.get('/*', function(req, res) {
  19. res.sendFile(path.join(__dirname, '/public/index.html'), function(err) {
  20. if (err) {
  21. res.status(500).send(err)
  22. }
  23. })
  24. });

huangapple
  • 本文由 发表于 2023年1月5日 01:42:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75009269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定