REST APIs
Comparison between JavaScript and Python
The information comes from the Udemy course "Become an AI Engineer: Python for JavaScript Developers" by Vilva Athiban
| JavaScript | Python |
|---|---|
Express
npm init make sure you use module instead of commonjs "type": "module", npm i express nodemon insert start script in package.json "server": "nodemon app.js" run with 'npm run server' stop with 'ctrl + c' // --- Basic endpoint ---
test with http://localhost:3000/
// --- Path parameter ---test with http://localhost:3000/books/2
// --- Query parameter (optional) ---test with http://localhost:3000/books?id=2
or http://localhost:3000/books
// --- POST with a JSON body ---// --- Middleware (must be injected manually per app / route) ---
|
Fast API
if new `uv init[projectName]` then cd into it `code.`
`uv add fastapi`
`uv add uvicorn`
Auto-generated interactive docs are served at `/docs` once the app is running
# Run: uvicorn main:app --reload (serves on http://localhost:8000)
# stop the uvicorn server : ctrl + c
# --- Basic endpoint (decorator = @app.get(path)) ---test with http://localhost:8000/
# --- Path parameter ---
# The magic of FastAPI: path params, query params, and JSON bodies ALL arrive
# as plain arguments to the handler. Just type-annotate them
# test with http://localhost:8000/books/2
# --- Query parameter (optional -> give it a default of None) ---
test with http://localhost:8000/books?id=2
test with http://localhost:8000/books
# --- POST with a JSON body (type it and it's parsed for you) ---# --- Middleware: one decorator makes it universal (applies to ALL endpoints) ---
|