diff --git a/.gitignore b/.gitignore index 54226043d37bcc71a101598f7e171f1d6b067597..62f586fa8c5e241d1d22f77c0661406183e20065 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ node_modules/ # Logs -logs +logs/ *.log npm-debug.log* yarn-debug.log* @@ -11,11 +11,14 @@ yarn-error.log* # Environment variables .env -# OS-specific files +# Build output +dist/ +build/ + +# Docker +docker-compose.override.yml +.dockerignore + +# System files .DS_Store Thumbs.db - -# IDE-specific files -.vscode/ -.idea/ -*.iml \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..b3de2962661430da48e87ad70cda92966ad0d2b4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +# Use the official Node.js image as the base image +FROM node:18 + +# Set the working directory inside the container +WORKDIR /app + +# Copy package.json and package-lock.json to the container +COPY package*.json ./ + +# Install dependencies +RUN npm install + +# Copy the rest of the application code to the container +COPY . . + +# Expose the application port +EXPOSE 3000 + +# Start the application +CMD ["node", "server.js"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000000000000000000000000000000000000..a5aab92c97e3eaeb3df9f3398f94e0135f76b5a9 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,22 @@ +services: + app: + build: + context: . + dockerfile: Dockerfile + ports: + - "3000:3000" # Expose the app on port 3000 + environment: + - MONGO_URL=mongodb://db:27017/todo-app # MongoDB connection string + depends_on: + - db + + db: + image: mongo:6.0 + container_name: mongodb + ports: + - "27017:27017" # Expose MongoDB on port 27017 + volumes: + - mongo-data:/data/db # Persist MongoDB data + +volumes: + mongo-data: \ No newline at end of file diff --git a/index.html b/public/index.html similarity index 100% rename from index.html rename to public/index.html diff --git a/style.css b/public/style.css similarity index 100% rename from style.css rename to public/style.css diff --git a/server.js b/server.js index 5acb3a994e9ccc7de836b3749539e3d51a0ba11e..28c9107f830db942c36d6054ca89126fb55b255e 100644 --- a/server.js +++ b/server.js @@ -2,6 +2,7 @@ const express = require('express'); const mongoose = require('mongoose'); const cors = require('cors'); +const path = require('path'); // Import path module for serving static files // Initialize Express app const app = express(); @@ -11,11 +12,11 @@ const PORT = 3000; // Define the port the server will run on app.use(express.json()); // Parse incoming JSON requests app.use(cors()); // Enable Cross-Origin Resource Sharing +// Serve static files from the "public" directory +app.use(express.static(path.join(__dirname, 'public'))); + // Connect to MongoDB database -mongoose.connect('mongodb://localhost:27017/todo-app', { - useNewUrlParser: true, - useUnifiedTopology: true, -}); +mongoose.connect(process.env.MONGO_URL || 'mongodb://localhost:27017/todo-app'); // Define the schema for a TODO item const todoSchema = new mongoose.Schema({ @@ -60,6 +61,11 @@ app.delete('/todos/:id', async (req, res) => { res.json({ message: 'Todo deleted' }); // Return a success message }); +// Default route to serve index.html +app.get('/', (req, res) => { + res.sendFile(path.join(__dirname, 'public', 'index.html')); +}); + // Start the server app.listen(PORT, () => { console.log(`Server running on http://localhost:${PORT}`); // Log the server URL