backend-for-frontend
is a architecture project structured to implement the Backend-for-Frontend (BFF) pattern. This architecture is designed to support separate backends for different frontend clients (web, mobile, etc.), ensuring tailored responses and performance optimization.
This monorepo manages:
- Web frontend (
frontend/web
) - Mobile frontend (
frontend/mobileApp
) - Web BFF (
backend/web-bff
) - Mobile BFF (
backend/mobile-bff
) - Shared services (
backend/shared
)
The project leverages Makefile
for build and dependency management and utilizes yarn
workspaces for dependency handling.
- BFF Architecture: Separate backend layers for Web and Mobile clients.
- Frontend Clients: React.js for web and React Native for mobile.
- Shared Services: Common utilities shared between BFFs.
- Isolated Dependency Management: Each project manages its own dependencies via
yarn
andMakefile
. - Concurrent Development: Ability to run multiple services simultaneously using
concurrently
. - Makefile Automation: Simplified commands for managing all services.
📦 backend
┣ 📂 _data
┃ ┗ 📜 db.json # JSON file for mock data
┣ 📂 mobile-bff # BFF tailored for the mobile client
┣ 📂 web-bff # BFF tailored for the web client
┣ 📂 shared # Shared backend code (e.g., validation, error handling)
┗ 📜 package.json # Backend dependencies
📦 frontend
┣ 📂 mobileApp # React Native application
┃ ┣ 📂 app
┃ ┃ ┣ 📂 Components # UI components (following atomic design principles)
┃ ┃ ┣ 📂 Screens # Login, RecipeList, RecipeDetail
┃ ┃ ┣ 📂 Context # Global states (e.g., AuthContext)
┃ ┃ ┗ 📂 Navigation # React Navigation setup
┣ 📂 web # React (web) application
┃ ┣ 📂 Components # Atomic components
┃ ┣ 📂 Pages # Login, RecipeList, RecipeDetail
┃ ┣ 📂 Context # Web global states
┃ ┗ 📜 index.tsx # Entry point for the React application
┗ 📜 package.json # Frontend dependencies
📦 node_modules # Project dependencies
📜 Makefile # Automation scripts
📜 package.json # Root dependencies
📜 README.md # Project documentation
📜 yarn.lock # Dependency version locking
Make sure the following are installed:
- Node.js (>= 18.x)
- Yarn (1.22.x)
- Make (for running commands)
Project Clone:
git clone https://github.com/jsdeveloperr/backend-for-frontend-architecture.git
cd backend-for-frontend-architecture
Run the following command to install dependencies for all projects:
yarn install:all
This executes:
make install
which installs dependencies for each subproject (web, mobile, shared, etc.).
To clean all node_modules
and reset the project:
yarn clean:all
This runs:
make clean
To clean the yarn cache:
yarn cache:clean
yarn start:backend
This runs:
make start-backends
It starts:
shared
web-bff
mobile-bff
yarn start:frontend
This runs:
make start-frontends
It starts:
web
mobile
React Native Debugging with Android
When running the mobile application on an Android device or emulator, API requests might fail due to localhost connectivity issues. To resolve this, you need to set up adb reverse commands.
Run the following commands:
adb reverse tcp:5000 tcp:5000 # Map Shared BFF API
adb reverse tcp:3001 tcp:3001 # Map Mobile BFF API
Service | Command |
---|---|
Web Frontend | yarn start:web |
Mobile Frontend | yarn start:mobile |
Web BFF Backend | yarn start:wbff |
Mobile BFF Backend | yarn start:mbff |
Shared Services | yarn start:shared |
Command | Description |
---|---|
yarn clean:all |
Cleans all node_modules and resets project |
yarn cache:clean |
Cleans the yarn cache |
For advanced users, all functionality is centralized in the Makefile
. Below are the available commands:
Command | Description |
---|---|
make install |
Installs dependencies for all projects |
make clean |
Cleans all node_modules directories |
make cache-clean |
Cleans yarn cache |
make start-web |
Starts the web frontend |
make start-mobile |
Starts the mobile frontend |
make start-shared |
Starts the shared services |
make start-backend-web-bff |
Starts the Web BFF backend |
make start-backend-mobile-bff |
Starts the Mobile BFF backend |
make start-backends |
Starts all backend services |
make start-frontends |
Starts all frontend services |
-
concurrently not found
- Run
yarn add concurrently -D
in the root directory.
- Run
-
Missing
node_modules
- Ensure
yarn install:all
was executed.
- Ensure
-
React Native Build Errors
- Ensure the correct setup for React Native by running:
npx react-native doctor
- Ensure the correct setup for React Native by running:
-
Port Conflicts
- Make sure no other process is using the default ports (e.g., 3000, 5000).
- Add Docker support for easier environment setup.
- Implement CI/CD pipelines.
- Enable remote caching for builds with TurboRepo.
We welcome contributions! Feel free to submit issues or pull requests to improve this project.
This project is licensed under the MIT License.