Next.js FastAPI Starter 项目技术文档
nextjs-fastapi 项目地址: https://gitcode.com/gh_mirrors/ne/nextjs-fastapi
1. 安装指南
1.1 环境准备
在开始安装之前,请确保您的开发环境已经安装了以下软件:
- Node.js (建议版本 14.x 或更高)
- Python (建议版本 3.7 或更高)
- npm 或 yarn 或 pnpm
1.2 克隆项目
您可以使用以下命令克隆项目到本地:
npx create-next-app nextjs-fastapi --example "https://github.com/digitros/nextjs-fastapi"
1.3 安装依赖
进入项目目录并安装依赖:
cd nextjs-fastapi
npm install
# 或者使用 yarn
yarn
# 或者使用 pnpm
pnpm install
2. 项目的使用说明
2.1 启动开发服务器
安装完依赖后,您可以使用以下命令启动开发服务器:
npm run dev
# 或者使用 yarn
yarn dev
# 或者使用 pnpm
pnpm dev
启动后,您可以在浏览器中访问 http://localhost:3000
查看 Next.js 前端应用。FastAPI 后端服务将在 http://127.0.0.1:8000
运行。
2.2 修改端口
如果您需要更改 FastAPI 服务器的端口,可以在 package.json
文件中修改 dev
脚本的端口,并在 next.config.js
文件中同步更新端口配置。
3. 项目API使用文档
3.1 API 路由
FastAPI 服务器的 API 路由映射到 Next.js 应用的 /api/
路径下。例如,如果您在 FastAPI 中定义了一个 /hello
路由,您可以通过 http://localhost:3000/api/hello
访问该路由。
3.2 API 示例
假设您在 FastAPI 中定义了以下路由:
from fastapi import FastAPI
app = FastAPI()
@app.get("/hello")
def read_root():
return {"message": "Hello World"}
您可以通过访问 http://localhost:3000/api/hello
来调用该 API,并返回 {"message": "Hello World"}
。
4. 项目安装方式
4.1 本地开发
如前所述,您可以通过克隆项目并安装依赖来在本地启动开发服务器。

4.2 部署到 Vercel
您可以通过以下按钮一键部署项目到 Vercel:
[](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fdigitros%2Fnextjs-fastapi%2Ftree%2Fmain)
在 Vercel 上部署后,FastAPI 服务器将以 Python 无服务器函数的形式运行。
5. 学习更多
5.1 Next.js 文档
- Next.js 文档 - 学习 Next.js 的功能和 API。
- 学习 Next.js - 一个交互式的 Next.js 教程。
5.2 FastAPI 文档
- FastAPI 文档 - 学习 FastAPI 的功能和 API。
通过以上文档,您可以更深入地了解 Next.js 和 FastAPI 的使用方法,并进一步扩展和优化您的项目。
nextjs-fastapi 项目地址: https://gitcode.com/gh_mirrors/ne/nextjs-fastapi