Learn how to build applications with FastAPI, React, and MongoDB in this FARM Stack course.

Posted by


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:

  1. 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
  2. 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.

  3. Set up a FastAPI project:
    Create a new directory for your FastAPI project and navigate into it. Create a new Python file called main.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
  1. Test the FastAPI server:
    Open your browser and navigate to http://localhost:8000/ to test the FastAPI server. You should see the message "Hello World" displayed on the page.

Setting up the Frontend with React:

  1. 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.

  2. 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
  3. Change into the project directory:
    Navigate into the project directory by running the following command:

    cd my-app
  4. 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
  5. Create a component to fetch data from the FastAPI server:
    Create a new file called DataComponent.js in the src 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;
  1. 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!

0 0 votes
Article Rating
25 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@Faybmi
1 month ago

40:12 if you don't have todo in mongo, try post method in docs

@laracombina
1 month ago

this was really helpful, thank you!!

@yinghouwang
1 month ago

Save my life!!!!

@yinghouwang
1 month ago

Thanks!

@rohitkumarb.r3865
1 month ago

What about update?

@Miho_13
1 month ago

Very good introduction , thank you !

@kz_cbble9670
1 month ago

24:00
33:00

@sarthakgautam8035
1 month ago

I am not able to import motor package …. tried upgrading all packages also still the same problem is there

@mahendranath2504
1 month ago

Wow, thank you so much for sharing the knowledge 🫡🙏👍😊

@naveens9575
1 month ago

That was an awesome short tutorial on farm stack✌

@LHM1226
1 month ago

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?

@himanshchauhan4287
1 month ago

Hi im having trouble. axios keeps calling backend api when fetching all todos in useeffect part. Is this expected?

@jon7035
1 month ago

Now we need something a T so we can have a FART Stack

@tyronefrielinghaus3467
1 month ago

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!

@Josh-qb4em
1 month ago

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.

@raminatox
1 month ago

The web dev had a FARM, E-I-E-I-O…

@dajlefromcb
1 month ago

If you are using Python 3.11, you have to use motor == 3.1.1 dependency.

@123arskas
1 month ago

Thank you so much for this. Now I can dive deeply into the FARM stack

@timbrap4693
1 month ago

Is this serverside rendering or clientside rendering?

@fumano2679
1 month ago

Im using angular as frontend but i really liked the backend part