60 lines
1.1 KiB
JavaScript
60 lines
1.1 KiB
JavaScript
const express = require('express');
|
|
const mongoose = require('mongoose');
|
|
const cors = require('cors');
|
|
const path = require('path');
|
|
|
|
const app = express();
|
|
|
|
app.use(express.json());
|
|
app.use(cors());
|
|
|
|
|
|
app.use(express.static(path.join(__dirname)));
|
|
|
|
mongoose.connect('mongodb://mongo:27017/todo');
|
|
|
|
const Task = mongoose.model('Task', {
|
|
text: String,
|
|
due: String
|
|
});
|
|
|
|
|
|
app.get('/', (req, res) => {
|
|
res.sendFile(path.join(__dirname, 'index.html'));
|
|
});
|
|
|
|
|
|
app.get('/tasks', async (req, res) => {
|
|
const tasks = await Task.find();
|
|
res.json(tasks);
|
|
});
|
|
|
|
|
|
app.post('/tasks', async (req, res) => {
|
|
const task = new Task({
|
|
text: req.body.text,
|
|
due: req.body.due
|
|
});
|
|
await task.save();
|
|
res.send("ok");
|
|
});
|
|
|
|
|
|
app.delete('/tasks/:id', async (req, res) => {
|
|
await Task.findByIdAndDelete(req.params.id);
|
|
res.send("deleted");
|
|
});
|
|
|
|
|
|
app.put('/tasks/:id', async (req, res) => {
|
|
await Task.findByIdAndUpdate(req.params.id, {
|
|
text: req.body.text,
|
|
due: req.body.due
|
|
});
|
|
res.send("updated");
|
|
});
|
|
|
|
app.listen(3000, () => {
|
|
console.log("backend bezi na porte 3000");
|
|
});
|