-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
42 lines (33 loc) · 1.03 KB
/
server.js
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
import express from 'express';
import bodyParser from 'body-parser';
import path from 'path';
import { fileURLToPath } from 'url';
const app = express();
const port = process.env.PORT || 3000;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public'))); // Correct path to static files
let tasks = [];
app.get('/', (req, res) => {
res.render('index', { tasks });
});
app.post('/add', (req, res) => {
const task = { description: req.body.task, completed: false };
tasks.push(task);
res.redirect('/');
});
app.post('/delete', (req, res) => {
const index = req.body.index;
tasks.splice(index, 1);
res.redirect('/');
});
app.post('/complete', (req, res) => {
const index = req.body.index;
tasks[index].completed = !tasks[index].completed;
res.redirect('/');
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});