-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
125 lines (114 loc) · 3.51 KB
/
index.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const express = require("express");
const app = express();
const db = require("./db");
const multer = require("multer");
const uidSafe = require("uid-safe");
const path = require("path");
const s3 = require("./s3");
const conf = require("./config");
const moment = require("moment");
const diskStorage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, __dirname + "/uploads");
},
filename: function(req, file, callback) {
uidSafe(24).then(function(uid) {
callback(null, uid + path.extname(file.originalname));
});
}
});
const uploader = multer({
storage: diskStorage,
limits: {
fileSize: 2097152
}
});
app.use(express.json());
app.use(express.static("public"));
app.get("/images", (req, res) => {
db.getImages()
.then(result => {
res.json(result.rows);
})
.catch(err => {
console.log(err);
});
});
app.get("/images/:id", (req, res) => {
console.log("Lowest ID: ", req.params.id);
db.getMoreImages(req.params.id)
.then(result => {
console.log("More images from database: ", result.rows);
res.json(result.rows);
})
.catch(err => {
console.log("Error getting more images from database: ", err);
res.sendStatus(500);
});
});
app.post("/upload", uploader.single("file"), s3.upload, (req, res) => {
console.log("file: ", req.file);
console.log("body: ", req.body);
// insert a row in the images table for the new image
let imageUrl = conf.s3Url + req.file.filename;
db.addImage(
imageUrl,
req.body.username,
req.body.title,
req.body.description
)
.then(image => {
console.log("image after insert into db: ", image);
res.json(image);
})
.catch(err => {
console.log(err);
res.sendStatus(500);
});
});
app.get("/image/:id", (req, res) => {
console.log("id: ", req.params.id);
db.getImageById(req.params.id)
.then(result => {
console.log("Image from db: ", result.rows);
result.rows[0].created_at = moment(
result.rows[0].created_at
).format("D/M/YYYY H:mm");
res.json(result.rows);
})
.catch(err => {
console.log("Error fetching image: ", err);
res.sendStatus(500);
});
});
app.post("/comment", (req, res) => {
console.log("Post comment req.body: ", req.body);
db.addComment(req.body.comment, req.body.username, req.body.imageId)
.then(result => {
console.log("Comment added to database: ", result);
res.json(result.rows);
})
.catch(err => {
console.log("Error adding comment to database: ", err);
res.sendStatus(500);
});
});
app.get("/comments/:imageId", (req, res) => {
db.getCommentsByImageId(req.params.imageId)
.then(result => {
for (let i = 0; i < result.rows.length; i++) {
result.rows[i].created_at = moment(
result.rows[i].created_at
).format("D/M/YYYY H:mm");
}
console.log("comments from db: ", result.rows);
res.json(result.rows);
})
.catch(err => {
console.log("Error fetching comments: ", err);
res.sendStatus(500);
});
});
app.listen(process.env.PORT || 8080, () =>
console.log("IB server listening...")
);