-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathserver.js
More file actions
52 lines (42 loc) · 1.54 KB
/
Copy pathserver.js
File metadata and controls
52 lines (42 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
// Connect to MongoDB (You need to have MongoDB installed and running)
mongoose.connect('mongodb://localhost/todo_list_db', { useNewUrlParser: true, useUnifiedTopology: true });
// Define a Task schema
const taskSchema = new mongoose.Schema({
name: String,
description: String,
dueDate: Date,
isCompleted: Boolean,
});
const Task = mongoose.model('Task', taskSchema);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
// API endpoints for CRUD operations
app.get('/api/tasks', async (req, res) => {
const tasks = await Task.find();
res.json(tasks);
});
app.post('/api/tasks', async (req, res) => {
const { name, description, dueDate } = req.body;
const task = new Task({ name, description, dueDate, isCompleted: false });
await task.save();
res.status(201).json(task);
});
app.put('/api/tasks/:id', async (req, res) => {
const taskId = req.params.id;
const { name, description, dueDate, isCompleted } = req.body;
const updatedTask = await Task.findByIdAndUpdate(taskId, { name, description, dueDate, isCompleted }, { new: true });
res.json(updatedTask);
});
app.delete('/api/tasks/:id', async (req, res) => {
const taskId = req.params.id;
await Task.findByIdAndRemove(taskId);
res.sendStatus(204);
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});