Add complete dockerized amigos app with React frontend and MongoDB backend
This commit is contained in:
1013
backend/package-lock.json
generated
Normal file
1013
backend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
18
backend/package.json
Normal file
18
backend/package.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "backend",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"type": "commonjs",
|
||||
"dependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"express": "^5.1.0",
|
||||
"mongodb": "^6.19.0"
|
||||
}
|
||||
}
|
||||
94
backend/server-prod.js
Normal file
94
backend/server-prod.js
Normal file
@@ -0,0 +1,94 @@
|
||||
const express = require('express');
|
||||
const { MongoClient } = require('mongodb');
|
||||
const cors = require('cors');
|
||||
const path = require('path');
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3001;
|
||||
|
||||
// Middleware
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
// Serve static files from React build
|
||||
app.use(express.static(path.join(__dirname, '..', 'dist')));
|
||||
|
||||
// MongoDB connection - using host.docker.internal for Docker networking
|
||||
const mongoHost = process.env.MONGO_HOST || 'host.docker.internal';
|
||||
const uri = `mongodb://admin:MongoPass2024!@${mongoHost}:27017/?authSource=admin`;
|
||||
const client = new MongoClient(uri);
|
||||
|
||||
let db;
|
||||
let amigosCollection;
|
||||
|
||||
// Connect to MongoDB
|
||||
async function connectDB() {
|
||||
try {
|
||||
await client.connect();
|
||||
console.log('Connected to MongoDB');
|
||||
db = client.db('testdb');
|
||||
amigosCollection = db.collection('amigos');
|
||||
} catch (error) {
|
||||
console.error('Error connecting to MongoDB:', error);
|
||||
// Retry connection after 5 seconds
|
||||
setTimeout(connectDB, 5000);
|
||||
}
|
||||
}
|
||||
|
||||
// API Routes
|
||||
// Get all amigos
|
||||
app.get('/api/amigos', async (req, res) => {
|
||||
try {
|
||||
if (!amigosCollection) {
|
||||
return res.status(503).json({ error: 'Database not connected' });
|
||||
}
|
||||
const amigos = await amigosCollection.find({}).toArray();
|
||||
res.json(amigos);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Add new amigo
|
||||
app.post('/api/amigos', async (req, res) => {
|
||||
try {
|
||||
if (!amigosCollection) {
|
||||
return res.status(503).json({ error: 'Database not connected' });
|
||||
}
|
||||
|
||||
const { nombre } = req.body;
|
||||
if (!nombre) {
|
||||
return res.status(400).json({ error: 'Nombre es requerido' });
|
||||
}
|
||||
|
||||
const result = await amigosCollection.insertOne({ nombre });
|
||||
res.status(201).json({
|
||||
message: 'Amigo agregado exitosamente',
|
||||
id: result.insertedId,
|
||||
nombre
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Health check endpoint
|
||||
app.get('/health', (req, res) => {
|
||||
res.json({
|
||||
status: 'ok',
|
||||
mongodb: amigosCollection ? 'connected' : 'disconnected'
|
||||
});
|
||||
});
|
||||
|
||||
// Serve React app for all other routes
|
||||
app.use((req, res) => {
|
||||
res.sendFile(path.join(__dirname, '..', 'dist', 'index.html'));
|
||||
});
|
||||
|
||||
// Start server
|
||||
connectDB().then(() => {
|
||||
app.listen(PORT, '0.0.0.0', () => {
|
||||
console.log(`Server running on http://0.0.0.0:${PORT}`);
|
||||
console.log(`MongoDB host: ${mongoHost}`);
|
||||
});
|
||||
});
|
||||
66
backend/server.js
Normal file
66
backend/server.js
Normal file
@@ -0,0 +1,66 @@
|
||||
const express = require('express');
|
||||
const { MongoClient } = require('mongodb');
|
||||
const cors = require('cors');
|
||||
|
||||
const app = express();
|
||||
const PORT = 3001;
|
||||
|
||||
// Middleware
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
// MongoDB connection
|
||||
const uri = 'mongodb://admin:MongoPass2024!@localhost:27017/?authSource=admin';
|
||||
const client = new MongoClient(uri);
|
||||
|
||||
let db;
|
||||
let amigosCollection;
|
||||
|
||||
// Connect to MongoDB
|
||||
async function connectDB() {
|
||||
try {
|
||||
await client.connect();
|
||||
console.log('Connected to MongoDB');
|
||||
db = client.db('testdb');
|
||||
amigosCollection = db.collection('amigos');
|
||||
} catch (error) {
|
||||
console.error('Error connecting to MongoDB:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Routes
|
||||
// Get all amigos
|
||||
app.get('/api/amigos', async (req, res) => {
|
||||
try {
|
||||
const amigos = await amigosCollection.find({}).toArray();
|
||||
res.json(amigos);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Add new amigo
|
||||
app.post('/api/amigos', async (req, res) => {
|
||||
try {
|
||||
const { nombre } = req.body;
|
||||
if (!nombre) {
|
||||
return res.status(400).json({ error: 'Nombre es requerido' });
|
||||
}
|
||||
|
||||
const result = await amigosCollection.insertOne({ nombre });
|
||||
res.status(201).json({
|
||||
message: 'Amigo agregado exitosamente',
|
||||
id: result.insertedId,
|
||||
nombre
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Start server
|
||||
connectDB().then(() => {
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server running on http://localhost:${PORT}`);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user