npx create-next-app@latest .
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def status():
return {"messge": "ok from uvicorn server"}
@app.get("/status")
def next_app():
return {"message": "from nextjs api"}
pip install fastapi
pip install uvicorn
pip install httpie
fastapi for using fastapi inside nextjs uvicorn to run the local server httpie to test our api endpoints in terminal
[!IMPORTANT]
/** @type {import('next').NextConfig} */
const nextConfig = {
rewrites: async () => {
return [
{
source: '/api/:path*',
destination:
process.env.NODE_ENV === 'development'
? 'http://127.0.0.1:8000/:path*'
: '/api/',
},
]
},
}
module.exports = nextConfig
- open terminal activate your virtual environment then run
npm run dev
This will start you nextjs development server at
localhost:3000
- Now in a new terminal run this command
uvicorn --app-dir api index:app --reload
This will start you fastapi uvicorn development server at
localhost:8000
httpie localhost:8000
OUTPUT
HTTP/1.1 200 OK
content-length: 35
content-type: application/json
date: Thu, 04 Jan 2024 19:49:39 GMT
server: uvicorn
{
"messge": "ok from uvicorn server"
}
This validates that our uvicorn server is working fine
http localhost:3000/api/status
OUTPUT
HTTP/1.1 200 OK
Vary: Accept-Encoding
connection: close
content-length: 29
content-type: application/json
date: Thu, 04 Jan 2024 19:52:00 GMT
server: uvicorn
{
"message": "from nextjs api"
}
This validates that our nextjs api is working fine
[!NOTE]
- This is optional step if you want to excute all the commands in one terminal add this npm package
npm install concurrently
- Replace scripts at the of your package.json file by the below code
"scripts": {
"dev": "concurrently \"npm run next-dev\" \"npm run fast-dev\"",
"fast-dev": "uvicorn --app-dir api index:app --reload",
"build": "next build",
"start": "next start",
"lint": "next lint",
"next-dev": "next dev"
},
Now when you run
npm run dev
your nextjs and uvicorn both server will start in one termial side by side