A step-by-step Node.js backend project built during the OUSL Backend Bootcamp. It starts from a plain Node.js server and grows into a REST API for managing books, using Express, MongoDB and Mongoose.
Each stage of the bootcamp lives on its own branch, so you can check out any branch to see the code exactly as it was at that step.
The slides from the workshop are in the root of the main branch, in two formats:
- PDF:
Back-End-Engineering-Workshop-Ashan-Dimantha.pdf. Opens in any PDF viewer and is the easiest to print or share. - HTML:
Back-End Engineering for Software Engineers Updates & Hands-On Foundations.html. Download it and open it in a web browser to view the slides. GitHub shows the HTML source rather than rendering it.
- Node.js: JavaScript runtime
- Express 5: web framework and routing
- MongoDB Atlas + Mongoose: database and ODM
- dotenv: loads environment variables from
.env - nodemon: restarts the server automatically when files change
Every branch starts from the initial commit and is a standalone snapshot of one stage. They are meant to be read in this order:
| # | Branch | What it covers |
|---|---|---|
| 1 | node-setup |
Plain Node.js HTTP server, reading PORT from .env |
| 2 | express-setup |
Express server with an in-memory books array |
| 3 | express-setup-refactor |
Express app split into routes and data modules |
| 4 | mongodb-setup |
Full CRUD API backed by MongoDB using Mongoose |
The starting point: a basic server built with Node's built-in http module, with no framework.
index.js: creates the server and listens onprocess.env.PORT- Introduces
.envandprocess.envfor configuration - Run with
node --env-file=.env index.js(nodotenvyet)
Replaces the raw Node server with Express.
- Adds
express,dotenvandnodemon(npm run dev) post.jskeeps books in an in-memory array:GET /books: list all booksPOST /books: add a book ({ "title": "..." })
- Uses
express.json()to parse JSON request bodies - Returns proper status codes (
201 Created,400 Bad Request)
The data is held in memory, so it resets every time the server restarts.
Restructures the Express app so the code is no longer all in one file.
index.js # app setup, middleware, mounts the router
routes/books.js # all book routes, using express.Router()
data/books.js # the in-memory books array
- Introduces
express.Router()andapp.use()to mount routes - Adds
GET /healthandGET /books/:idwith a404when the book doesn't exist
Moves storage from memory to MongoDB, so data persists between restarts.
index.js # connects to MongoDB, starts Express
models/book.js # Mongoose schema: title (required), author
repositories/bookRepository.js # all database calls in one place
routes/books.js # HTTP routes that call the repository
- Connects with
mongoose.connect(process.env.MONGO_URL) - Adds a repository layer, so routes never talk to Mongoose directly
- Full CRUD with
async/await(see API below) - Books are identified by MongoDB's
_id(for example6aeda4ea69122780eed68bc4), not a number
git clone https://github.com/devwithashan/ousl-backend-bootcamp.git
cd ousl-backend-bootcamp
git checkout mongodb-setup # or any branch from the table above
npm installCreate a .env file in the project root:
PORT=3001
MONGO_URL=mongodb+srv://<user>:<password>@<cluster>.mongodb.net/<database>MONGO_URL is only needed on mongodb-setup. Never commit .env. It holds your database credentials.
Run the server:
npm run dev # with nodemon, restarts on file changesYou should see:
Server running at 3001
MongoDB connected!
Stop the server with Ctrl+C. Ctrl+Z only pauses it. The port stays taken and requests hang without a response.
Base URL: http://localhost:3001
| Method | Endpoint | Body | Description |
|---|---|---|---|
GET |
/health |
Health check, returns { "status": "ok" } |
|
GET |
/books |
List all books | |
GET |
/books/:id |
Get one book, 404 if not found |
|
POST |
/books |
{ "title": "...", "author": "..." } |
Create a book, returns 201 |
PATCH |
/update/:id |
any fields to change | Update a book |
DELETE |
/delete/:id |
Delete a book, 404 if not found |
Create a book:
curl -X POST http://localhost:3001/books \
-H "Content-Type: application/json" \
-d '{"title": "Atomic Habits", "author": "James Clear"}'{ "title": "Atomic Habits", "author": "James Clear", "_id": "6aeda4ea69122780eed68bc4", "__v": 0 }Get, update and delete it using the returned _id:
curl http://localhost:3001/books/6aeda4ea69122780eed68bc4
curl -X PATCH http://localhost:3001/update/6aeda4ea69122780eed68bc4 \
-H "Content-Type: application/json" \
-d '{"title": "New Book"}'
curl -X DELETE http://localhost:3001/delete/6aeda4ea69122780eed68bc4In a URL, use the id itself: /books/6aeda4..., not /books/:6aeda4.... The :id form is only used when defining the route in code.