Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
npm-debug.log
Dockerfile
.dockerignore
.gitignore
121 changes: 95 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,95 @@
# Book Store MERN stack project

## Lessons

- [x] Lesson 01: Create Node.js project from scratch
- [x] Lesson 02: Create our first Http Route
- [x] Lesson 03: Add MongoDB and mongoose to node js
- [x] Lesson 04: Create Book model with mongoose
- [x] Lesson 05: Save a new Book with mongoose
- [x] Lesson 06: Get All Books with mongoose
- [x] Lesson 07: Get One Book by id with mongoose
- [x] Lesson 08: Update a Book with mongoose
- [x] Lesson 09: Delete a book with mongoose
- [x] Lesson 10: Refactor Node js with express router
- [x] Lesson 11: CORS policy in Node js and Express js
- [x] Lesson 12: Create React project, Vite, Tailwind CSS
- [x] Lesson 13: SPA and Add react router dom
- [x] Lesson 14: Show Books List in React
- [x] Lesson 15: Show Book Details in React
- [x] Lesson 16: Create Book in React
- [x] Lesson 17: Edit Book in React
- [x] Lesson 18: Delete Book in React
- [x] Lesson 19: Show Books List as Card
- [x] Lesson 20: Make Book Card a single component
- [x] Lesson 21: Add Book Modal
- [x] Lesson 22: Improve User Experience (UX) with beautiful alert
# Book Store MERN Stack Application

This is a full-stack MERN (MongoDB, Express, React, Node.js) application for managing a book store. The application allows users to create, read, update, and delete books.

## Prerequisites

- Node.js
- Docker
- Docker Compose

## Getting Started

### Backend

1. Navigate to the `backend` directory:

```sh
cd backend
```

2. Install the dependencies:

```sh
npm install
```

3. Create a `.env` file in the [backend](http://_vscodecontentref_/1) directory and add the following environment variables:

```env
PORT=5555
MONGO_URL=mongodb://localhost:27017/books-collection
```

4. Start the backend server:

```sh
npm start
```

### Frontend

1. Navigate to the [frontend](http://_vscodecontentref_/2) directory:

```sh
cd frontend
```

2. Install the dependencies:

```sh
npm install
```

3. Start the frontend development server:

```sh
npm run dev
```

### Docker

.1 Build and start the services using Docker Compose:

```sh
docker-compose up --build
```

2. Access the frontend application at `http://localhost:5173` and the backend API at `http://localhost:5555`.

## API Endpoints

### Books

- `POST /books`: Create a new book
- `GET /books`: Get all books
- `GET /books/:id`: Get a book by ID
- `PUT /books/:id`: Update a book by ID
- `DELETE /books/:id`: Delete a book by ID

## Frontend Pages

- `/`: Home page displaying the list of books
- `/books/create`: Create a new book
- `/books/details/:id`: View book details
- `/books/edit/:id`: Edit a book
- `/books/delete/:id`: Delete a book

## Authors

- Yashraj Singh Solanki
- MamadTaheri68

## License

This project is licensed under the ISC License.
5 changes: 5 additions & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
npm-debug.log
Dockerfile
.dockerignore
.gitignore
24 changes: 24 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Base Image
FROM node:16

# metadata

LABEL maintainer="yes20sh@outloo.com"
LABEL author="Yashraj Singh Solanki"
LABEL version="1.0"
LABEL description="Dockerfile for the backend of the book store MERN stack application"

# setting working directory
WORKDIR /app

# copy package.json and installing the dependancies
COPY package.json package-lock.json ./
RUN npm install

COPY . .

# Expose port
EXPOSE 3000

# start application
CMD ["npm","start"]
12 changes: 8 additions & 4 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import express from 'express';
import { PORT, mongoDBURL } from './config.js';
import mongoose from 'mongoose';
import booksRoute from './routes/booksRoute.js';
import cors from 'cors';
import booksRoute from './routes/booksRoute.js';

const app = express();
const PORT = process.env.PORT || 3000;
const mongoDBURL = process.env.MONGO_URL || 'mongodb://localhost:27017/default-db';

// Middleware for parsing request body
app.use(express.json());
Expand All @@ -29,13 +30,16 @@ app.get('/', (request, response) => {
app.use('/books', booksRoute);

mongoose
.connect(mongoDBURL)
.connect(mongoDBURL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log('App connected to database');
app.listen(PORT, () => {
console.log(`App is listening to port: ${PORT}`);
});
})
.catch((error) => {
console.log(error);
console.log('MongoDB connection error:', error);
});
21 changes: 21 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

# Update package list and install Docker
sudo apt-get update
sudo apt-get install -y docker.io

# Install Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

# Clone the project repository from GitHub
git clone https://github.com/your-username/Book-Store-MERN-Stack.git

# Navigate to the project directory
cd Book-Store-MERN-Stack

# Build and start the Docker containers using Docker Compose
sudo docker-compose up --build -d

# Print the status of the Docker containers
sudo docker-compose ps
61 changes: 61 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
version: '3.8'

services:
frontend:
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- "5173:5173"
environment:
- NODE_ENV=development
depends_on:
mongodb:
condition: service_healthy
networks:
- book-store-network
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:5173"]
interval: 30s
timeout: 10s
retries: 5

backend:
build:
context: ./backend
dockerfile: Dockerfile
ports:
- "5555:3000"
environment:
- NODE_ENV=development
- MONGO_URL=mongodb://root:root@mongodb:27017/books-collection?authSource=admin
depends_on:
mongodb:
condition: service_healthy
networks:
- book-store-network
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000"]
interval: 30s
timeout: 10s
retries: 5

mongodb:
image: mongo:latest
container_name: mongodb
ports:
- "27017:27017"
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=root
networks:
- book-store-network
healthcheck:
test: ["CMD", "mongo", "--eval", "db.adminCommand('ping')"]
interval: 30s
timeout: 10s
retries: 5

networks:
book-store-network:
driver: bridge
5 changes: 5 additions & 0 deletions frontend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
npm-debug.log
Dockerfile
.dockerignore
.gitignore
26 changes: 26 additions & 0 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Dockerfile for frontend

# Base Image
FROM node:16

# Metadata
LABEL maintainer="yes20sh@outlook.com"
LABEL version="1.0"
LABEL author="Yashraj Singh Solanki"
LABEL description="Dockerfile for the frontend of the book store MERN stack application"

# Setting working directory
WORKDIR /app

# Copy the package.json and package-lock.json (if available) and install dependencies
COPY package.json package-lock.json ./
RUN npm install

# Copying the rest of the application code
COPY . .

# Expose the Port
EXPOSE 5173

# Start the application
CMD ["npm", "run", "dev", "--", "--host"]
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"start": "vite --host",
"dev": "vite",
"build": "vite build",
"lint": "eslint src --ext js,jsx --report-unused-disable-directives --max-warnings 0",
Expand Down