-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
89 lines (80 loc) · 2.39 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
const express = require("express");
const app = express();
const moment = require("moment");
const { uploader } = require("./uploads");
const {
getImages,
insertImage,
getImageData,
getAllComments,
insertComment,
getMoreImages,
} = require("./db");
const s3 = require("./s3");
app.use(express.static("./public"));
app.use(express.json());
app.post("/upload", uploader.single("file"), s3.upload, (req, res) => {
const { title, username, description } = req.body;
const url = `https://spicedling.s3.amazonaws.com/${req.file.filename}`;
if (req.file) {
insertImage(url, username, title, description)
.then((data) => {
console.log("testtestets", data);
res.json(data.rows[0]);
})
.catch(console.log);
} else {
res.json({ success: false });
}
});
app.get("/images", (req, res) => {
getImages()
.then((data) => {
res.json(data);
})
.catch(console.log);
});
app.get("/get-image/:id", (req, res) => {
getImageData(req.params.id)
.then((data) => {
data.rows[0].created_at = moment(data.rows[0].created_at).fromNow();
res.json(data);
})
.catch(console.log());
});
app.post("/comment.json", (req, res) => {
const { comment, username, image_id } = req.body;
insertComment(comment, username, image_id)
.then(
(data) => {
console.log(data);
data.rows[0].created_at = moment(
data.rows[0].created_at
).fromNow();
res.json(data.rows[0]);
}
// console.log("Kartoffel", data)
// res.json({ image_id, comment, username })
)
.catch(console.log);
});
app.get("/comments/:imageId.json", (req, res) => {
getAllComments(req.params.imageId)
.then((data) => {
data.rows.forEach(
(image) =>
(image.created_at = moment(image.created_at).fromNow())
);
res.json(data);
})
.catch(console.log);
});
app.get("/get-more-images/:id", (req, res) => {
getMoreImages(req.params.id)
.then((data) => res.json(data))
.catch(console.log);
});
app.get("*", (req, res) => {
res.sendFile(`${__dirname}/index.html`);
});
app.listen(8080, () => console.log(`I'm listening.`));