-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (99 loc) · 3.31 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
const express = require("express");
const app = express();
const db = require("./sql/db.js");
app.use(express.static("public"));
app.use(express.json());
// boilerplate file upload
const multer = require('multer');
const uidSafe = require('uid-safe');
const path = require('path');
// multer is storing the files in our upload folder
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));
});
}
});
//ROUTES//
// uploader runs our discStorage that needs to be up to 2MB
const uploader = multer({
storage: diskStorage,
limits: {
fileSize: 2097152
}
});
// communication between the server and axios
app.get("/images", (req, res)=>{
db.getImages().then(result=>{
res.json(result.rows);
}).catch(error=>{
console.log("Error in GET request:", error);
});
});
// S3
const s3 = require("./s3.js");
const {s3Url} = require("./config.json");
app.get("/images", (req, res)=>{
db.getImages().then(result=>{
res.json(result.rows);
}).catch(error=>{
console.log("Error in GET:", error);
});
});
// a route for listening requests for files
app.post("/upload", uploader.single("file"), s3.upload, (req, res)=>{
const {filename} = req.file;
const url = `${s3Url}${filename}`;
const {title, description, username} = req.body;
// at this point, we have sent the image to Amazon and have a URL it is
// accessible under. We want to re-render the entire list of images,
// with the new image in front.
//
// We do this by inserting the new image using the data we have about
// it, specifically including the Amazon-provided URL, which will give
// it the highest current serial# in the db table. This, combined with
// our ORDER BY ID DESC, will put the newest image first.
//
// Once the image is inserted, we do a simple repeat of the entire
// GET route. We could break it out into a separate function for
// theoretically drier code, but with five lines total, it's
// questionable if this improves readability and maintainability.
db.insertIntoImages(url, title, description, username).then(({rows})=>{
return res.json(rows[0]);
}).then((result) => {
// This part is a verbatim repeat of the GET route
db.getImages().then(result=>{
}).catch(error=>{
console.log("Error in GET request:", error);
});
}).catch(error=>{
console.log(error);
});
});
app.get('/image/:id', (req, res) => {
db.getDataFromImages(req.params.id).then(result => {
res.json(result.rows[0]);
}).catch(err => {
console.log(err);
});
});
app.get('/comments/:id', (req, res) => {
db.getComments(req.params.id).then(result => {
res.json(result.rows);
}).catch(err => {
console.log(err);
});
});
app.post('/comment', (req, res) => {
const { imageId, username, comment } = req.body;
db.addComment(imageId, username, comment).then(({ rows }) => {
res.json(rows[0]);
}).catch(err => {
console.log(err);
});
});
app.listen(8080, ()=> console.log("listening"));