This is a sample project demonstrating how to use Bun server with MongoDB to create a simple movie database API.
- Bun installed on your system
- A MongoDB database (you can use MongoDB Atlas for a free cloud-hosted option)
-
Clone this repository:
git clone https://github.com/tooniez/bun-api-mongodb cd bun-api-mongodb
-
Install dependencies:
bun install
-
Set up your environment variables: Copy
.env.example
file in the root directory and add your MongoDB connection string:cp .env.example .env MONGODB_URI=<your_mongodb_connection_string>
-
Run the application:
bun run index.ts
GET /movies
: Fetch all moviesGET /movies/:id
: Fetch a specific movie by IDPOST /movies
: Add a new moviePUT /movies/:id
: Update a movieDELETE /movies/:id
: Delete a movie
index.ts
: Main application file with route handlerscontrollers/movies.ts
: Movie controller with CRUD operationsmodels/movies.ts
: Movie model definitionutils/db.ts
: Database connection utility
If you prefer to use Docker, you can run the project using the oven/bun
image: