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

Merge branch '3-set-up-server-and-api-routes' into 'main'

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

Closes #3

See merge request !3
parents 99f997e7 9b019758
No related branches found
No related tags found
1 merge request!3Adding server.js functionality and main operations for todos.js
Pipeline #15544 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 path = require('path');
const connectDB = require('./mongodb'); // Import the MongoDB connection
const app = express();
// Basic route for testing
app.get('/', (req, res) => {
res.send('Server is running!');
});
// Middleware
app.use(express.json()); // Use express' built-in body-parser for JSON
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
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
app.listen(PORT, () => console.log(`Server 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