-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
94 lines (78 loc) · 2.34 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
const express = require("express");
const moment = require("moment");
const app = express();
const {
getImages,
addImage,
getImgByID,
addComment,
getCommentsByID,
getMoreImagesByID
} = require("./db");
const { uploader } = require("./upload");
const s3 = require("./s3");
app.use(express.static("./public"));
app.use(express.json());
app.post("/upload", uploader.single("file"), s3.upload, (req, res) => {
if (req.file) {
const fileName = req.file.filename;
const urlToSaveInDB = `https://s3.amazonaws.com/spicedling/${fileName}`;
addImage(
req.body.description,
req.body.username,
req.body.title,
urlToSaveInDB
)
.then(({ rows }) => {
console.log("image successfully saved in db");
res.json(rows);
})
.catch((err) => {
console.log("error adding img to db", err);
res.sendStatus(500);
});
} else {
// TODO: what do we do if success is false??
res.json({ success: false });
}
});
app.get("/get-imageboard-data", (req, res) => {
getImages().then(({ rows }) => {
res.json(rows);
});
});
app.get("/get-img-by-id-data/:id", (req, res) => {
getImgByID(req.params.id).then(({ rows }) => {
let dateAdded = moment(rows[0].created_at).fromNow();
rows[0].dateAdded = dateAdded;
res.json(rows);
});
});
app.get("/get-more-images/:id", (req, res) => {
getMoreImagesByID(req.params.id).then(({ rows }) => {
res.json(rows);
});
});
app.get("/comments/:id", (req, res) => {
getCommentsByID(req.params.id).then(({ rows }) => {
rows.forEach( row => {
let dateAddedComment = moment(row.created_at).fromNow();
row.dateAddedComment = dateAddedComment;
});
res.json(rows);
});
});
app.post("/upload-comment", (req, res) => {
addComment(req.body.username, req.body.comment, req.body.img_id)
.then(({ rows }) => {
res.json(rows);
})
.catch((err) => {
console.log("error adding comment into db", err);
res.sendStatus(500);
});
});
app.get("*", (req, res) => {
res.sendFile(`${__dirname}/index.html`);
});
app.listen(8080, () => console.log(`I'm listening.`));