-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
102 lines (95 loc) · 2.63 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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const { createClient } = require("@supabase/supabase-js");
const config = require("./config/supabaseCreds");
let express = require("express");
let app = express();
app.use(express.json());
const cors = require("cors");
app.use(cors());
app.get("/comments/:id", async (req, res) => {
try {
const postid = req.params["id"];
const supabase = createClient(config.PROJECT_URL, config.API_KEY);
const { data, error } = await supabase
.from("comments")
.select()
.eq("postid", postid)
.order("date", { ascending: false });
if (error) {
throw new Error(error.message);
}
res.status(200).json(data);
} catch (error) {
console.error(error.message);
res.status(500).json(error.message);
}
});
app.post("/newpost", async (req, res) => {
try {
const body = req.body;
const supabase = createClient(config.PROJECT_URL, config.API_KEY);
const { data, error } = await supabase
.from("entries")
.insert([
{
postid: body.postid,
nickname: body.nickname,
header: body.header,
content: body.content,
category: body.category,
date: new Date().toISOString(),
instagramProfileUrl: body.instagramProfileUrl
},
])
.select();
if (error) {
throw new Error(error.message);
}
res.status(200).json("Success");
} catch (error) {
console.error(error.message);
res.status(500).json(error.message);
}
});
app.post("/newcomment", async (req, res) => {
try {
const body = req.body;
const supabase = createClient(config.PROJECT_URL, config.API_KEY);
const { data, error } = await supabase
.from("comments")
.insert([
{
commentid: body.commentid,
postid: body.postid,
comment: body.comment,
nickname: body.nickname,
date: new Date().toISOString(),
instagramProfileUrl: body.instagramProfileUrl
},
])
.select();
if (error) {
throw new Error(error.message);
}
res.status(200).json("Success");
} catch (error) {
console.error(error.message);
res.status(500).json(error.message);
}
});
app.get("/", async (req, res) => {
try {
const supabase = createClient(config.PROJECT_URL, config.API_KEY);
const { data, error } = await supabase
.from("entries")
.select()
.order("date", { ascending: false });
if (error) {
throw new Error(error.message);
}
res.status(200).json(data);
} catch (error) {
console.error(error.message);
res.status(500).json(error.message);
}
});
app.listen(process.env.PORT || 4000);