Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Full Stack FastAPI, React, and MongoDB

You're reading from   Full Stack FastAPI, React, and MongoDB Fast-paced web app development with the FARM stack

Arrow left icon
Product type Paperback
Published in Aug 2024
Publisher Packt
ISBN-13 9781835886762
Length 312 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (4):
Arrow left icon
Shrey Batra Shrey Batra
Author Profile Icon Shrey Batra
Shrey Batra
Rachelle Palmer Rachelle Palmer
Author Profile Icon Rachelle Palmer
Rachelle Palmer
Shubham Ranjan Shubham Ranjan
Author Profile Icon Shubham Ranjan
Shubham Ranjan
Marko Aleksendrić Marko Aleksendrić
Author Profile Icon Marko Aleksendrić
Marko Aleksendrić
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Chapter 1: Web Development and the FARM Stack FREE CHAPTER 2. Chapter 2: Setting Up the Database with MongoDB 3. Chapter 3: Python Type Hints and Pydantic 4. Chapter 4: Getting Started with FastAPI 5. Chapter 5: Setting Up a React Workflow 6. Chapter 6: Authentication and Authorization 7. Chapter 7: Building a Backend with FastAPI 8. Chapter 8: Building the Frontend of the Application 9. Chapter 9: Third-Party Services Integration with FastAPI and Beanie 10. Chapter 10: Web Development with Next.js 14 11. Chapter 11: Useful Resources and Project Ideas 12. Index 13. Other Books You May Enjoy

What is the FARM stack?

Stacks are sets of technologies that cover different parts of a modern web app, blended and well integrated. The right stack will enable you to satisfy certain criteria while building a web application, with considerably less effort and in less time than building it from scratch.

First, let’s see what you need to build a functional web application:

  • An operating system: Usually, this is Unix/Linux-based.
  • A storage layer: A SQL or NoSQL database. In this book, we’ll use MongoDB.
  • A web server: Apache and NGINX are quite popular, but we will talk about Python solutions for FastAPI, such as Uvicorn or Hypercorn.
  • A development environment: Node.js/JavaScript, .NET, Java, or Python.

Optionally, and often, you could also add a frontend library or framework (such as Vue.js, Angular, React, or Svelte) since the vast majority of web development companies benefit from adopting one in terms of consistency, development speed, and compliance with standards. In addition, user expectations have shifted over time. There are unsaid standards for what logins, buttons, menus, and other website elements should look like, and how they function. Using a framework will make your application more consistent with the modern web and go a long way toward user satisfaction.

The most famous stacks are as follows:

  • MERN: MongoDB + Express.js + React + Node.js (MERN) is probably one of the most popular stacks today. Developers can be comfortable and never leave JavaScript, except when they need to write some style sheets. With the addition of React Native for mobile apps and something such as Electron.js for desktop apps, a product can encompass virtually every platform while relying solely on JavaScript.
  • MEAN: MongoDB + Express.js + Angular.js + Node.js (MEAN) is similar to the previously mentioned MERN, with Angular.js managing the frontend in a more structured Model–View–Controller (MVC) way.
  • LAMP: Linux + Apache + MySQL + PHP (LAMP) is probably the first stack acronym to gain popularity and one of the most widely used in the past 20 years. It is still quite popular today.

The first two stacks run on the Node.js platform (a server-run JavaScript V8 engine) and have a web framework in common. Although Express.js is the most popular, there are excellent alternatives in the Node.js universe, such as Koa.js, Fastify.js, or some more structured ones such as Nest.js.

Since this is a Python book, we will also go through some important Python frameworks. The top three most popular frameworks for Python developers are Django, Flask, and FastAPI. Using the Django web framework and the excellent Django REST Framework (DRF) for building REST APIs in a modern and logical way is very popular. Django itself is very mature and well known among Python developers. It also has an admin site, the possibility of customizing and serializing REST responses, the option to choose between functional and class-based views, and more.

FastAPI, on the other hand, is a relative newcomer. First released in December 2018, this alternative, lightweight framework was fast to gain advocates. Almost immediately, these advocates had created a new acronym for FastAPI within the tech stack—FARM.

Let’s understand what FARM stands for:

  • FA stands for FastAPI—in technology years, a brand-new Python web framework
  • R stands for React, the most popular UI library
  • M denotes the data layer—MongoDB, which is the most popular NoSQL database available today

Figure 1.1 provides a high-level overview of the integrations between the constituent parts involved in the FARM stack:

Figure 1.1 – A Diagram of the FARM stack with its components

Figure 1.1: FARM stack with its components

As you can see in the preceding diagram, the FARM stack is composed of three layers:

  1. The user performs an action using the client, which, in our case, will be based on React—this ultimately creates a bundle of HTML, Cascading Style Sheets (CSS), and JavaScript.
  2. This user action (a mouse click, a form submit, or some other event) then triggers an HTTP request (such as GET, POST, PUT, or another HTTP verb with a payload).
  3. Finally, this request gets processed by the REST API service (FastAPI).

The Python part is centered around FastAPI and optional dependencies and is served by Uvicorn—a fast Python-based server. The backend is responsible for dispatching the appropriate database calls to MongoDB using various commands and queries (such as findOne, find, create, update, and more) and using the MongoDB aggregation framework. The results obtained from the database are interpreted by FastAPI through the Python driver of choice (Motor), converted from BSON into appropriate Python data structures, and finally, output from the REST API server in the form of plain JSON. If you use Motor, which is an asynchronous Python driver for MongoDB, these calls will be handled asynchronously.

Finally, returning to the diagram in Figure 1.1 and the arrow labeled JSON, the data is fed to the UI where it is handled by React and used to update the interface, render the necessary components, and synchronize the UI with React’s virtual DOM tree.

The next few sections will talk about the motivations behind the birth of the FARM stack. Why these technologies and, more importantly, why these technologies together? You will get a detailed introduction to each component and the features that make it a good fit in more detail. After a brief introduction to the benefits of the stack as a whole, the sections will provide a high-level overview of each choice and underline the benefits that it can provide to a modern web development workflow.

You have been reading a chapter from
Full Stack FastAPI, React, and MongoDB - Second Edition
Published in: Aug 2024
Publisher: Packt
ISBN-13: 9781835886762
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image