Building Social Media Apps · Course book
Chapter 4: A Full-Stack Microblogging App
A microblogging app such as a Twitter-style clone is a complete full-stack system in miniature. A mobile app lets people sign in, post text and images, like, comment, search, chat and see notifications; a REST API stores and serves all of it; a database keeps it; an image service hosts the pictures; and a security layer stops abuse. This chapter explains how those parts are designed and connected, following a project built with React Native and Expo for the app, Express and MongoDB for the API, Clerk for authentication, Cloudinary for images and Arcjet for rate limiting and bot protection. It also covers the team workflow with Git branches and pull requests, and what it takes to deploy the API so a real phone can use it.
Architecture and the team workflow
The system has two codebases. The backend is a Node.js server using Express, which defines routes such as GET /api/posts or POST /api/posts/:postId/like, reads and writes a MongoDB database through the Mongoose library, and returns JSON. The mobile app, built with React Native and Expo, renders native screens on iOS and Android, calls those routes over HTTPS and displays the results. Authentication is delegated to Clerk, which handles sign-in with providers such as Google or Apple and gives the app a token to send with every request; the backend verifies the token before trusting the request.
Working like a team from the start pays off. The main branch always holds working code. Each feature is built on its own branch, pushed to GitHub and merged through a pull request, a page where changes are reviewed, discussed and, increasingly, checked by automated review tools before they are merged. Small, focused pull requests are easier to review and easier to undo if something goes wrong.
Data models and the REST API
MongoDB stores documents, JSON-like records, in collections. Mongoose schemas describe the shape of each kind of document and add validation. A design along the lines of the project uses four models, sketched below with typical fields and routes; the exact names in your own code may differ.
| Model | Main fields | Example routes |
|---|---|---|
| User | clerkId, email, username, name, profile and banner pictures, bio, followers, following | GET /api/users/profile/:username, PUT /api/users/profile, POST /api/users/follow/:targetUserId |
| Post | user, content, image, likes, comments, timestamps | GET /api/posts, POST /api/posts, POST /api/posts/:postId/like, DELETE /api/posts/:postId |
| Comment | user, post, content, likes | POST /api/comments/post/:postId, DELETE /api/comments/:commentId |
| Notification | from, to, type (follow, like or comment), post, comment | GET /api/notifications, DELETE /api/notifications/:id |
Two modelling choices are worth understanding. Relationships are stored as references, the ids of other documents, and filled in when needed with Mongoose's populate, so that a post can be returned with its author's name and picture in one response. Lists such as a post's likes are stored as arrays of user ids, which makes "has this user liked this?" a quick membership check; the like route adds the id if it is absent and removes it if present, toggling the like with one button. Each route that changes data first checks that the requester is signed in and, for edits and deletions, that they own the item, returning 401 Unauthorized or 403 Forbidden otherwise.
Images are not stored in the database. When a post includes a picture, the backend receives the file, uploads it to Cloudinary, an image hosting and transformation service, and saves only the returned address in the post. Cloudinary can resize and compress images on the fly, so the app downloads small versions for the feed and larger ones only when needed.
Protecting the API: rate limits and bots
A public API must withstand abuse: scripts that flood it with requests, bots that create spam, and simple mistakes such as an app stuck in a loop. Rate limiting caps how many requests a client may make in a period. A common algorithm is the token bucket: each client has a bucket holding up to a fixed number of tokens; each request spends one; tokens are added back at a steady rate; and a request that finds the bucket empty is rejected with status 429 Too Many Requests.
Services such as Arcjet package rate limiting together with bot detection, which recognises automated clients from the patterns in their requests, and protections against common attacks, applied as Express middleware that runs before every route. Legitimate traffic from the mobile app passes through, while abusive traffic is stopped before it reaches the database. After the API is ready, it is deployed to a hosting platform with its environment variables, such as the database address and secret keys, set on the server rather than written into the code.
The mobile app: navigation, authentication and data
The app uses Expo, which provides a development server, a phone app for instant previews and build tools for the stores. NativeWind brings Tailwind-style utility classes to React Native, so styles such as flex-1 bg-white px-4 are written inline as class names. Expo Router uses folders to define screens: an authentication group for sign-in, and a tab group for the main app with five tabs, Home, Search, Notifications, Messages and Profile. If no user is signed in, the router redirects to the sign-in screen; after signing in with Clerk, the app opens on the tabs, and signing out returns to sign-in.
When a user signs in for the first time, the app calls a sync route so the backend creates a matching User document from the Clerk account. From then on, every request includes the Clerk token in an Authorization header, attached automatically by a small API client built on a library such as axios. Server data is fetched and cached with a data-fetching library such as React Query, which handles loading and error states, caches results and refetches after a change. After a like, for example, invalidating the posts query makes the feed reload with the new count.
Screens: posting, comments, notifications and profiles
The Home screen has a composer at the top for writing a post and attaching an image, picked from the gallery or taken with the camera through Expo's image picker, which asks the user's permission first. Below it, the feed lists posts, each with like, comment and delete actions, the last shown only on the user's own posts. Tapping comment opens a modal listing the comments and a box for adding one. Pull-to-refresh reloads the feed.
Feeds grow without limit, so returning every post in one response does not scale. A common refinement is pagination: the API returns, say, 20 posts at a time, newest first, together with a cursor such as the creation time of the last post sent, and the app requests the next page when the user scrolls near the bottom. React Native's FlatList component suits this well, because it renders only the rows currently on screen and offers a callback when the end of the list is approaching. Showing a small spinner at the bottom while the next page loads tells the user that more is on the way.
The Notifications screen lists likes, comments and follows aimed at the user, created by the backend whenever those actions happen. The Messages screen shows conversations with chat history and supports deleting a conversation with a long press, a common mobile gesture. The Search screen shows trending topics. The Profile screen shows the banner, picture, bio, follower counts and the user's posts, and an edit modal updates the profile through the API. Handling empty and error states on every screen, such as "No notifications yet" or a retry button when the network fails, is what makes the app feel finished.
Practice tasks
- A bucket holds 20 tokens and refills at 2 per second. A client sends 25 requests at once, then 10 more after 4 seconds. How many requests are rejected in total?
- Write the steps the like route takes, including the ownership and sign-in checks, and what it returns.
- Why are images uploaded to an image service and only their addresses stored in the database?
- Explain what happens, from the app to the database, when a new user signs in for the first time.
- Which HTTP status codes should the API return for: not signed in; deleting someone else's post; too many requests; a post that does not exist?
- Describe a branch and pull request workflow for adding the Search screen.
This course book is available to read online only at tech-academy.chrysolites.org.