-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (73 loc) · 2.06 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
const db = require("./utils/db");
const express = require("express");
const app = express();
const s3 = require("./s3");
const config = require("./config");
const multer = require("multer");
const uidSafe = require("uid-safe");
const path = require("path");
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: 2897152
}
});
app.use(express.static("public"));
app.use(express.json());
app.get("/images", (req, res) => {
db.getImages().then(result => {
res.json(result);
});
});
app.get("/moreimages/:id", (req, res) => {
db.getMoreImages(req.params.id).then(result => {
res.json(result);
});
});
app.get("/image/:id", (req, res) => {
db.getImage(req.params.id)
.then(result => {
res.json(result);
})
.catch(err => {
console.log("err", err);
});
});
app.get("/comments/:id", (req, res) => {
db.getComments(req.params.id).then(result => {
res.json(result);
});
});
app.post("/upload", uploader.single("file"), s3.upload, (req, res) => {
const { filename } = req.file;
const url = config.s3Url + filename;
const { title, username, description } = req.body;
db.insertImages(url, username, title, description)
.then(result => {
res.json(result);
})
.catch(err => {
console.log("uploading error", err);
});
});
app.post("/comment", (req, res) => {
const { comment, username, id } = req.body;
db.insertComment(comment, username, id)
.then(result => {
res.json(result);
})
.catch(err => {
console.log("uploading error", err);
});
});
app.listen(8080, () => console.log("My image board server is Up!"));