diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..f387c273 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +node_modules +npm-debug.log +Dockerfile +.dockerignore +.gitignore \ No newline at end of file diff --git a/README.md b/README.md index 93ae17c2..c26e7a42 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file +# 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. \ No newline at end of file diff --git a/backend/.dockerignore b/backend/.dockerignore new file mode 100644 index 00000000..f387c273 --- /dev/null +++ b/backend/.dockerignore @@ -0,0 +1,5 @@ +node_modules +npm-debug.log +Dockerfile +.dockerignore +.gitignore \ No newline at end of file diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 00000000..f8e4266b --- /dev/null +++ b/backend/Dockerfile @@ -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"] diff --git a/backend/index.js b/backend/index.js index 69c10978..c6226ca3 100644 --- a/backend/index.js +++ b/backend/index.js @@ -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()); @@ -29,7 +30,10 @@ 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, () => { @@ -37,5 +41,5 @@ mongoose }); }) .catch((error) => { - console.log(error); + console.log('MongoDB connection error:', error); }); diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 00000000..8ff26455 --- /dev/null +++ b/deploy.sh @@ -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 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..5429fb87 --- /dev/null +++ b/docker-compose.yml @@ -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 \ No newline at end of file diff --git a/frontend/.dockerignore b/frontend/.dockerignore new file mode 100644 index 00000000..f387c273 --- /dev/null +++ b/frontend/.dockerignore @@ -0,0 +1,5 @@ +node_modules +npm-debug.log +Dockerfile +.dockerignore +.gitignore \ No newline at end of file diff --git a/frontend/Dockerfile b/frontend/Dockerfile new file mode 100644 index 00000000..d435cac8 --- /dev/null +++ b/frontend/Dockerfile @@ -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"] \ No newline at end of file diff --git a/frontend/package.json b/frontend/package.json index 8bf237b9..27d14d9c 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -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",