Skip to content
Snippets Groups Projects
Commit 9b019758 authored by Jesus Galaz's avatar Jesus Galaz
Browse files

Adding server.js functionality and main operations for todos.js

parent 99f997e7
No related branches found
No related tags found
1 merge request!3Adding server.js functionality and main operations for todos.js
Pipeline #15536 passed
const express = require('express');
const router = express.Router();
const Todo = require('../models/todo');
// Get all TODOs for the logged-in user
router.get('/', async (req, res) => {
const todos = await Todo.find({ userId: req.user.id });
res.json(todos);
});
// Add a new TODO
router.post('/', async (req, res) => {
const { description } = req.body;
const todo = new Todo({ description, userId: req.user.id });
await todo.save();
res.json(todo);
});
// Mark TODO as done
router.put('/:id', async (req, res) => {
const todo = await Todo.findById(req.params.id);
todo.isDone = true;
await todo.save();
res.json(todo);
});
// Delete a TODO
router.delete('/:id', async (req, res) => {
await Todo.findByIdAndDelete(req.params.id);
res.json({ success: true });
});
module.exports = router;
// server.js
const express = require('express'); const express = require('express');
const path = require('path');
const connectDB = require('./mongodb'); // Import the MongoDB connection
const app = express(); const app = express();
// Basic route for testing // Middleware
app.get('/', (req, res) => { app.use(express.json()); // Use express' built-in body-parser for JSON
res.send('Server is running!'); app.use(express.static(path.join(__dirname, 'public'))); // Serve static files
});
// Connect to MongoDB
connectDB(); // Call the function to establish the database connection
// Routes
app.use('/api/todos', require('./routes/todos'));
app.use('/api/users', require('./routes/users'));
// Start the server // Start the server
const PORT = process.env.PORT || 3000; const PORT = process.env.PORT || 3000;
app.listen(PORT, () => { app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
console.log(`Server is running on port ${PORT}`);
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment