The FARM stack, which stands for FastAPI, React, and MongoDB, is a modern and powerful stack that combines the best technologies for building full-stack applications. FastAPI is a Python web framework that is known for its high performance and ease of use. React is a JavaScript library for building user interfaces, and MongoDB is a NoSQL database that is widely used for its flexibility and scalability. In this tutorial, we will walk you through setting up and building a full-stack application using the FARM stack.
Setting up the Backend with FastAPI and MongoDB:
-
Install FastAPI:
To get started, you will need to install FastAPI. You can do this by running the following command in your terminal:pip install fastapi
-
Install MongoDB:
Next, you will need to install MongoDB. You can follow the instructions on the MongoDB website to download and install MongoDB on your machine. - Set up a FastAPI project:
Create a new directory for your FastAPI project and navigate into it. Create a new Python file calledmain.py
and add the following code:from fastapi import FastAPI from pymongo import MongoClient
app = FastAPI()
client = MongoClient(‘mongodb://localhost:27017/’)
db = client[‘mydatabase’]
collection = db[‘mycollection’]
@app.get("/")
async def read_root():
return {"message": "Hello World"}
4. Start the FastAPI server:
To start the FastAPI server, run the following command in your terminal:
```bash
uvicorn main:app --reload
- Test the FastAPI server:
Open your browser and navigate tohttp://localhost:8000/
to test the FastAPI server. You should see the message "Hello World" displayed on the page.
Setting up the Frontend with React:
-
Install Node.js and npm:
If you haven’t already, you will need to install Node.js and npm on your machine. You can download and install them from the Node.js website. -
Create a new React project:
Create a new directory for your React project and navigate into it. Run the following command in your terminal to create a new React project:npx create-react-app my-app
-
Change into the project directory:
Navigate into the project directory by running the following command:cd my-app
-
Install Axios:
To make HTTP requests from your React app, you will need to install the Axios library. You can do this by running the following command in your terminal:npm install axios
- Create a component to fetch data from the FastAPI server:
Create a new file calledDataComponent.js
in thesrc
directory of your React project and add the following code:import React, { useState, useEffect } from 'react'; import axios from 'axios';
const DataComponent = () => {
const [data, setData] = useState(null);
useEffect(() => {
axios.get('http://localhost:8000/')
.then(response => {
setData(response.data);
})
.catch(error => {
console.log(error);
});
}, []);
return (
<div>
<h1>Data from FastAPI server</h1>
{data && <p>{data.message}</p>}
</div>
);
};
export default DataComponent;
6. Use the DataComponent in your main App component:
Open the `App.js` file in the `src` directory of your React project and import the `DataComponent`. Update the `App` component to include the `DataComponent` like this:
```javascript
import React from 'react';
import DataComponent from './DataComponent';
function App() {
return (
<div>
<DataComponent />
</div>
);
}
export default App;
- Start the React development server:
To start the React development server, run the following command in your terminal:npm start
Testing the Full-Stack Application:
Now that you have set up the backend with FastAPI and MongoDB and the frontend with React, you can test the full-stack application. Open your browser and navigate to http://localhost:3000/
to see the data fetched from the FastAPI server and displayed in your React app.
In this tutorial, you have learned how to set up and build a full-stack application using the FARM stack. FastAPI provides a high-performance backend, React offers a powerful frontend, and MongoDB delivers a flexible database solution. By combining these technologies, you can build modern, scalable, and efficient full-stack applications. Experiment with different features and functionalities to enhance your application further. Happy coding!
40:12 if you don't have todo in mongo, try post method in docs
this was really helpful, thank you!!
Save my life!!!!
Thanks!
What about update?
Very good introduction , thank you !
24:00
33:00
I am not able to import motor package …. tried upgrading all packages also still the same problem is there
Wow, thank you so much for sharing the knowledge 🫡🙏👍😊
That was an awesome short tutorial on farm stack✌
I can view the data in the react frontend. But I cannot do post / delete request
Error code for the delete request:
Network Error
AxiosError: Network Error
at XMLHttpRequest.handleError (http://localhost:3000/static/js/bundle.js:42015:14)
Error code for the post request:
Request failed with status code 405
AxiosError: Request failed with status code 405
at settle (http://localhost:3000/static/js/bundle.js:43284:12)
at XMLHttpRequest.onloadend (http://localhost:3000/static/js/bundle.js:41966:66)
Is anyone facing the same problem here?
Hi im having trouble. axios keeps calling backend api when fetching all todos in useeffect part. Is this expected?
Now we need something a T so we can have a FART Stack
great : and you have a good , easy to follow voice. Some FCC courses have AWFUL voices , super hard to listen to for long….high extrinsic cognitive load imposed!
I have this issue when watching tutorial videos like this where if the instructor makes a typo or something, I'll be hyper-fixated on it until it gets acknowledged. Anything said/taught between the time of the typo and the time of acknowledgment goes in one ear and out the other. So the fact that you did those little popups immediately acknowledging it was wonderful.
The web dev had a FARM, E-I-E-I-O…
If you are using Python 3.11, you have to use motor == 3.1.1 dependency.
Thank you so much for this. Now I can dive deeply into the FARM stack
Is this serverside rendering or clientside rendering?
Im using angular as frontend but i really liked the backend part