diff --git a/Dockerfile b/backend/Dockerfile similarity index 100% rename from Dockerfile rename to backend/Dockerfile diff --git a/models/todo.js b/backend/models/todo.js similarity index 100% rename from models/todo.js rename to backend/models/todo.js diff --git a/models/user.js b/backend/models/user.js similarity index 100% rename from models/user.js rename to backend/models/user.js diff --git a/mongodb.js b/backend/mongodb.js similarity index 100% rename from mongodb.js rename to backend/mongodb.js diff --git a/package-lock.json b/backend/package-lock.json similarity index 100% rename from package-lock.json rename to backend/package-lock.json diff --git a/package.json b/backend/package.json similarity index 100% rename from package.json rename to backend/package.json diff --git a/routes/todos.js b/backend/routes/todos.js similarity index 57% rename from routes/todos.js rename to backend/routes/todos.js index f853d478a09a9afa8ae8f76464bc82c367f6989f..f684d7718ebc5690f6412ffd4dda03ecfab0e122 100644 --- a/routes/todos.js +++ b/backend/routes/todos.js @@ -1,79 +1,78 @@ -const express = require('express'); +const express = require("express"); const router = express.Router(); -const Todo = require('../models/todo'); +const Todo = require("../models/todo"); // Middleware to verify if the user is authenticated function isAuthenticated(req, res, next) { if (req.session.user) { return next(); } else { - return res.status(401).json({ error: 'Unauthorized' }); + return res.status(401).json({ error: "Unauthorized" }); } } // Route to logout -router.post('/logout', (req, res) => { +router.post("/logout", (req, res) => { req.session.destroy((err) => { if (err) { - console.error('Error al destruir la sesión:', err); - return res.status(500).json({ error: 'Error al cerrar sesión' }); + console.error("Error al destruir la sesión:", err); + return res.status(500).json({ error: "Error al cerrar sesión" }); } - res.status(200).json({ message: 'Logout successful' }); + res.status(200).json({ message: "Logout successful" }); }); }); // Route to obtain all tasks of authenticated user -router.get('/', isAuthenticated, async (req, res) => { +router.get("/", isAuthenticated, async (req, res) => { try { const todos = await Todo.find({ userId: req.session.user.id }); res.json(todos); } catch (error) { console.error(error); - res.status(500).json({ error: 'Server error' }); + res.status(500).json({ error: "Server error" }); } }); // Route to add a task -router.post('/', isAuthenticated, async (req, res) => { +router.post("/", isAuthenticated, async (req, res) => { const { description } = req.body; - + try { const todo = new Todo({ description, userId: req.session.user.id }); await todo.save(); res.json(todo); } catch (error) { console.error(error); - res.status(500).json({ error: 'Server error' }); + res.status(500).json({ error: "Server error" }); } }); - // Route to eliminate tasks -router.delete('/:id', isAuthenticated, async (req, res) => { +router.delete("/:id", isAuthenticated, async (req, res) => { try { const todo = await Todo.findById(req.params.id); if (!todo || todo.userId.toString() !== req.session.user.id) { - return res.status(404).json({ error: 'Todo not found or unauthorized' }); + return res.status(404).json({ error: "Todo not found or unauthorized" }); } - await todo.deleteOne(); + await todo.deleteOne(); res.json({ success: true }); } catch (error) { console.error(error); - res.status(500).json({ error: 'Server error' }); + res.status(500).json({ error: "Server error" }); } }); // Route to update the description of a task -router.put('/:id/description', isAuthenticated, async (req, res) => { +router.put("/:id/description", isAuthenticated, async (req, res) => { const { description } = req.body; - + try { const todo = await Todo.findById(req.params.id); - + if (!todo || todo.userId.toString() !== req.session.user.id) { - return res.status(404).json({ error: 'Todo not found or unauthorized' }); + return res.status(404).json({ error: "Todo not found or unauthorized" }); } todo.description = description; @@ -82,19 +81,19 @@ router.put('/:id/description', isAuthenticated, async (req, res) => { res.json({ todo }); } catch (error) { console.error(error); - res.status(500).json({ error: 'Server error' }); + res.status(500).json({ error: "Server error" }); } }); // Route to mark a task as completed -router.put('/:id/done', isAuthenticated, async (req, res) => { +router.put("/:id/done", isAuthenticated, async (req, res) => { const { isDone } = req.body; - + try { const todo = await Todo.findById(req.params.id); if (!todo || todo.userId.toString() !== req.session.user.id) { - return res.status(404).json({ error: 'Todo not found or unauthorized' }); + return res.status(404).json({ error: "Todo not found or unauthorized" }); } todo.isDone = isDone; @@ -103,8 +102,8 @@ router.put('/:id/done', isAuthenticated, async (req, res) => { res.json({ todo }); } catch (error) { console.error(error); - res.status(500).json({ error: 'Server error' }); + res.status(500).json({ error: "Server error" }); } }); -module.exports = router; \ No newline at end of file +module.exports = router; diff --git a/routes/users.js b/backend/routes/users.js similarity index 100% rename from routes/users.js rename to backend/routes/users.js diff --git a/backend/server.js b/backend/server.js new file mode 100644 index 0000000000000000000000000000000000000000..54b2a4c01c2bda769ffa0afc0f51451a22c31074 --- /dev/null +++ b/backend/server.js @@ -0,0 +1,31 @@ +const express = require("express"); +const path = require("path"); +const connectDB = require("./mongodb"); +const session = require("express-session"); + +const app = express(); + +// Middleware +app.use(express.json()); +app.use(express.static(path.join(__dirname, "public"))); + +// Session configuration +app.use( + session({ + secret: "session_secret", + resave: false, + saveUninitialized: false, + cookie: { secure: false }, + }) +); + +// Connection to MongoDB +connectDB(); + +// Routes +app.use("/api/todos", require("./routes/todos")); +app.use("/api/users", require("./routes/users")); + +// Initialize the server +const PORT = process.env.PORT || 3000; +app.listen(PORT, () => console.log(`Server running on port ${PORT}`)); diff --git a/public/css/index.css b/frontend/public/css/index.css similarity index 100% rename from public/css/index.css rename to frontend/public/css/index.css diff --git a/public/css/login.css b/frontend/public/css/login.css similarity index 100% rename from public/css/login.css rename to frontend/public/css/login.css diff --git a/public/css/main.css b/frontend/public/css/main.css similarity index 100% rename from public/css/main.css rename to frontend/public/css/main.css diff --git a/public/index.html b/frontend/public/index.html similarity index 100% rename from public/index.html rename to frontend/public/index.html diff --git a/public/js/app.js b/frontend/public/js/app.js similarity index 100% rename from public/js/app.js rename to frontend/public/js/app.js diff --git a/public/js/auth.js b/frontend/public/js/auth.js similarity index 100% rename from public/js/auth.js rename to frontend/public/js/auth.js diff --git a/public/js/authorization.js b/frontend/public/js/authorization.js similarity index 100% rename from public/js/authorization.js rename to frontend/public/js/authorization.js diff --git a/public/js/index.js b/frontend/public/js/index.js similarity index 100% rename from public/js/index.js rename to frontend/public/js/index.js diff --git a/public/login.html b/frontend/public/login.html similarity index 100% rename from public/login.html rename to frontend/public/login.html diff --git a/public/register.html b/frontend/public/register.html similarity index 100% rename from public/register.html rename to frontend/public/register.html diff --git a/server.js b/server.js deleted file mode 100644 index 8dc73f18130077caa361219022688b47c55e8c21..0000000000000000000000000000000000000000 --- a/server.js +++ /dev/null @@ -1,31 +0,0 @@ -const express = require('express'); -const path = require('path'); -const connectDB = require('./mongodb'); -const session = require('express-session'); - -const app = express(); - -// Middleware -app.use(express.json()); -app.use(express.static(path.join(__dirname, 'public'))); - -// Session configuration -app.use( - session({ - secret: 'session_secret', - resave: false, - saveUninitialized: false, - cookie: { secure: false } - }) -); - -// Connection to MongoDB -connectDB(); - -// Routes -app.use('/api/todos', require('./routes/todos')); -app.use('/api/users', require('./routes/users')); - -// Initialize the server -const PORT = process.env.PORT || 3000; -app.listen(PORT, () => console.log(`Server running on port ${PORT}`));