password
查看人数
type
status
date
slug
summary
category
icon
tags
作者
状态
 
如果你想在 FastAPI 中直接集成 React,而不做前后端分离,你可以按照以下步骤进行:

项目结构


1. FastAPI 处理 React 静态文件

FastAPI 可以直接提供 React 编译后的静态文件。修改 main.py

2. React 配置

frontend/package.json 中修改 build 输出目录,让 React 直接输出到 backend/static/

3. 构建 & 运行

  1. 构建 React
    1. 运行 FastAPI
      1. 访问 http://localhost:8000/,FastAPI 会返回 React 的 index.html

      4. 开发时

      • 前端开发: 运行 npm start,开发时使用 React 开发服务器。
      • 后端开发: 运行 uvicorn,后端代码变更时自动刷新。
      这样,你可以在不完全分离前后端的情况下,同时享受 React 的开发体验和 FastAPI 的后端服务。
       
      💡
      有关使用上的问题,欢迎您在底部评论区留言,一起交流~
      V2ray节点github同步gitlab代码,gitee同步GitHub
      Loading...