-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
124 lines (118 loc) · 3.53 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
const express = require("express");
const {
getImages,
writeFiletodb,
getImageFromDB,
getComments,
writeCommentstodb,
getMoreImagesFromdb
} = require("./imageboarddb");
const s3 = require("./s3");
const config = require("./config");
const app = express();
app.use(require("body-parser").json()); // used in POST requests
/***********************************************************************/
/* File Upload header Declarations */
/***********************************************************************/
var multer = require("multer");
var uidSafe = require("uid-safe");
var path = require("path");
var diskStorage = multer.diskStorage({
/*destination: directory to save the files to*/
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));
});
}
});
var uploader = multer({
storage: diskStorage,
limits: {
fileSize: 2097152
}
});
/***********************************************************************/
app.use(express.static("public"));
app.get("/getImages", (req, res) => {
getImages()
.then(function(results) {
res.json({ images: results.rows });
})
.catch(function(err) {
console.log("error in getting images !", err);
});
});
app.get("/getComments/:id", (req, res) => {
let imageId = req.params.id;
getComments(imageId)
.then(function(results) {
res.json({ comments: results.rows });
})
.catch(function(err) {
console.log("error in getting images !", err);
});
});
app.get("/getImage/:id", (req, res) => {
let imageId = req.params.id;
getImageFromDB(imageId)
.then(function(results) {
res.json({ image: results.rows });
})
.catch(function(err) {
console.log("error in getting the image based on id!", err);
res.json({
image: {}
});
});
});
app.get("/getMoreImages/:id", (req, res) => {
let lastimageid = req.params.id;
getMoreImagesFromdb(lastimageid)
.then(function(results) {
res.json({ moreimages: results.rows });
})
.catch(function(err) {
console.log("error in getting mores image based on lastid!", err);
res.status(500).json({
success: false
});
});
});
app.post("/upload", uploader.single("file"), s3.upload, (req, res) => {
console.log("POST upload!", req.body);
writeFiletodb(
config.s3Url + req.file.filename,
req.body.title,
req.body.description,
req.body.username
)
.then(({ rows }) => {
res.json({
image: rows[0]
});
})
.catch(() => {
res.status(500).json({
success: false
});
});
});
app.post("/submitComment/:id", (req, res) => {
let imageId = req.params.id;
writeCommentstodb(imageId, req.body.comment, req.body.username)
.then(results => {
res.json({
comment: results.rows
});
})
.catch(err => {
console.log("error from submit comments", err);
res.status(500).json({
success: false
});
});
});
app.listen(8080, () => console.log("listening on port 8080..."));