-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (53 loc) · 1.6 KB
/
index.js
File metadata and controls
70 lines (53 loc) · 1.6 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const express = require('express');
const supabase = require('./supabaseClient');
const path = require('path');
const app = express();
const PORT = 3000;
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
// GET /tasks
app.get('/tasks', async (req, res) => {
const { data, error } = await supabase
.from('tasks')
.select('*')
.order('created_at', { ascending: false });
if (error) return res.status(500).json({ error: error.message });
res.json(data);
});
// POST /tasks
app.post('/tasks', async (req, res) => {
const { title } = req.body;
if (!title) return res.status(400).json({ error: 'Title is required' });
const { data, error } = await supabase
.from('tasks')
.insert([{ title }])
.select()
.single();
if (error) return res.status(500).json({ error: error.message });
res.status(201).json(data);
});
// PUT /tasks/:id
app.put('/tasks/:id', async (req, res) => {
const id = parseInt(req.params.id);
const { title } = req.body;
const { data, error } = await supabase
.from('tasks')
.update({ title })
.eq('id', id);
if (error) return res.status(400).json({ error: error.message });
res.json(data);
});
// DELETE /tasks/:id
app.delete('/tasks/:id', async (req, res) => {
const id = parseInt(req.params.id);
const { error } = await supabase
.from('tasks')
.delete()
.eq('id', id);
if (error) return res.status(500).json({ error: error.message });
res.json({ message: `Task ${id} deleted` });
});
// Start server
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});