This guide will walk you through setting up a simple application with ElysiaJS as your backend API and React as your frontend, using Bun for a fast development experience.
Goal:
Create an ElysiaJS backend server on port 3000.
Expose a simple API endpoint (/api/hello).
Create a React frontend on port 5173 (Vite default).
Fetch data from the ElysiaJS backend and display it in the React app.
Prerequisites
Bun: Make sure you have Bun installed. It's used for both package management and running our projects.
bash
curl -fsSL https://bun.sh/install | bash
(Or follow instructions on bun.sh)
1. Project Setup
Let's create a monorepo-like structure with backend and frontend folders.
bash
mkdir elysia-react-quickstart
cd elysia-react-quickstart
1.1 Create the ElysiaJS Backend
bash
bun create elysia backend
cd backend
bun install # Ensure all dependencies are installed
cd ..
1.2 Create the React Frontend (with Vite)
bash
bun create vite frontend --template react-ts # or react for JavaScript
cd frontend
bun install # Ensure all dependencies are installed
cd ..
Now, let's set up our ElysiaJS backend to serve some data.
Navigate to the backend directory:
bash
cd backend
Install CORS Plugin:
Since our frontend (React) will run on a different port than the backend, we need to enable Cross-Origin Resource Sharing (CORS).
bash
bun add @elysiajs/cors
Modify src/index.ts:
Open backend/src/index.ts and update its content to include CORS and a simple API endpoint.
typescript
import { Elysia } from 'elysia'
import { cors } from '@elysiajs/cors' // Import the CORS plugin
const app = new Elysia()
.use(cors()) // Use the CORS plugin to allow cross-origin requests
.get("/", () => "Hello Elysia!")
.get("/api/hello", () => {
return { message: "Hello from ElysiaJS backend!", timestamp: new Date().toISOString() };
})
.listen(3000, ({ hostname, port }) => {
console.log(`🦊 Elysia is running at http://${hostname}:${port}`);
});
export type App = typeof app;
Run the Backend:
bash
bun run src/index.ts
# Or, if you prefer the dev server with hot reload:
# bun dev
You should see 🦊 Elysia is running at http://localhost:3000.
Open your browser and visit http://localhost:3000/api/hello. You should see the JSON response:
{"message":"Hello from ElysiaJS backend!","timestamp":"..."}
3. Frontend: React App
Now, let's modify our React app to fetch data from the ElysiaJS backend.
Navigate to the frontend directory:
bash
cd ../frontend
Modify src/App.tsx (or src/App.jsx if you chose JavaScript):
Open frontend/src/App.tsx and replace its content with the following:
To see everything working together, you'll need two terminal windows:
Terminal 1 (Backend):
bash
cd elysia-react-quickstart/backend
bun dev
Terminal 2 (Frontend):
bash
cd elysia-react-quickstart/frontend
bun dev
With both running, navigate to http://localhost:5173/ in your browser. You should see your React application displaying the message fetched from the ElysiaJS backend.
What's Next?
Error Handling: Implement more robust error handling on both ends.
More Endpoints: Add POST, PUT, DELETE endpoints to your ElysiaJS app.
React Components: Break down your React app into smaller, reusable components.
State Management: For more complex apps, consider libraries like Zustand, Jotai, or Redux for React.
Database: Integrate a database (e.g., PostgreSQL with Drizzle ORM, SQLite with better-sqlite3) into your ElysiaJS backend.
Authentication: Add user authentication to your API.
Deployment: Learn how to deploy your ElysiaJS backend and React frontend to a cloud provider.
Monorepo Tools: For larger projects, consider tools like Turborepo or Nx to manage your backend and frontend as part of a single monorepo.