-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
155 lines (139 loc) · 3.98 KB
/
Copy pathserver.js
File metadata and controls
155 lines (139 loc) · 3.98 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const express = require("express");
const {
getImages,
addImage,
getImageInfo,
getComments,
addComment,
getMoreImages,
getThread,
addReply,
} = require("./db");
const app = express();
const multer = require("multer");
const uidSafe = require("uid-safe");
const path = require("path");
const { upload } = require("./s3");
const { DataBrew } = require("aws-sdk");
const { s3Url } = require("./config");
const { nextTick } = require("process");
app.use(
// express.urlencoded({
// extended: false,
// })
express.json()
);
app.use(express.static("./public"));
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));
});
},
});
// limit on the file size!
const uploader = multer({
storage: diskStorage,
limits: {
fileSize: 2097152,
},
});
app.post("/upload", uploader.single("file"), upload, (req, res) => {
const { username, title, description, id } = req.body;
addImage(s3Url + req.file.filename, username, title, description)
.then((data) => {
console.log("successful db entry");
return res.json({
url: s3Url + req.file.filename,
username: username,
title: title,
description: description,
id: data.rows[0].id,
});
})
.catch((err) => {
console.log("error in db entry", err);
});
});
app.get("/board", (req, res) => {
console.log("hit the board route");
getImages()
.then((data) => {
res.json(data.rows);
})
.catch((err) => {
console.log(err);
});
// res.json means we are no longer using res.render
// res.render is used for rendering templates, in a SPA Vue is the boss.
// the server is just getting stuff from the database and sending it back to Vue.
});
app.get("/image/:id", (req, res) => {
getImageInfo(req.params.id)
.then((data) => {
// console.log(data.rows[0]);
res.json(data.rows[0]);
})
.catch((err) => {
console.log("error in getImageInfo: ", err);
});
});
app.get("/comments/:id", (req, res) => {
getComments(req.params.id)
.then((data) => {
res.json(data.rows);
})
.catch((err) => {
console.log("error in getImageInfo: ", err);
});
});
app.post("/comment", (req, res) => {
const { name, comment, id } = req.body;
addComment(name, comment, id)
.then(({ rows }) => {
res.json(rows[0]);
})
.catch((err) => {
console.log("error in comment POST", err);
});
});
app.get("/moreimages/:id", (req, res) => {
console.log("req.params", req.params.id);
getMoreImages(req.params.id)
.then((data) => {
console.log(
"lowestId from data.rows[0].lowestId",
data.rows[0].lowestId
);
res.json(data.rows);
})
.catch((err) => {
console.log("error in getMoreImages: ", err);
});
});
app.get("/replies/:id", (req, res) => {
console.log("getting replies!");
getThread(req.params.id)
.then((data) => {
console.log(data.rows);
res.json(data.rows);
})
.catch((err) => {
console.log("error getting thread: ", err);
});
});
app.post("/replies/", (req, res) => {
console.log("posting reply");
const { reply, username, commentId } = req.body;
addReply(reply, username, commentId)
.then((data) => {
res.json(data.rows[0]);
})
.catch((err) => {
console.log("error in addreply");
});
});
app.listen(8080, () => console.log("listening on 8080..."));