-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
38 lines (31 loc) Β· 987 Bytes
/
server.js
File metadata and controls
38 lines (31 loc) Β· 987 Bytes
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
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
require('dotenv').config();
const app = express();
app.use(cors());
app.use(express.json());
const Task = mongoose.model('Task', new mongoose.Schema({
title: String,
completed: Boolean
}));
app.get('/tasks', async (req, res) => {
const tasks = await Task.find();
res.json(tasks);
});
app.post('/tasks', async (req, res) => {
const task = new Task({ title: req.body.title, completed: false });
await task.save();
res.json(task);
});
app.put('/tasks/:id', async (req, res) => {
const task = await Task.findByIdAndUpdate(req.params.id, { completed: true }, { new: true });
res.json(task);
});
app.delete('/tasks/:id', async (req, res) => {
await Task.findByIdAndDelete(req.params.id);
res.json({ success: true });
});
mongoose.connect(process.env.MONGO_URI).then(() => {
app.listen(5000, () => console.log("Backend running on http://localhost:5000"));
});