-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
120 lines (104 loc) · 3.64 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
const express = require("express");
const app = express();
const db = require("./utils/db");
const bodyParser = require("body-parser");
app.use(express.static("./public"));
app.use(bodyParser.json());
const moment = require('moment');
moment.locale('en');
//------------------------has to be above routes, handles file uploads
var multer = require("multer");
var uidSafe = require("uid-safe");
var path = require("path");
const s3 = require("./s3");
const config = require("./config");
//-------- multer saves the file to uploads directory -------//
//filename function tells multer to use as the file name the unique id generated by the call to uidSafe with the extension of the original file name appended to it//
var 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));
});
}
});
var uploader = multer({
storage: diskStorage,
limits: {
fileSize: 2097152
}
});
//---------------------------- Part1 -------------------
app.get("/images", function(req, res) {
db.getImages()
.then(images => {
// console.log("RESULTS:", images);
res.json(images);
})
.catch(err => {
console.log("error", err);
});
});
//-------------------LOAD MORE IMAGES --------------------
app.get("/more/:lastId", function(req, res) {
// console.log("req.params", req.params.lastId);
db.getMoreImages(req.params.lastId).then(results => {
res.json(results.rows);
});
});
//--------------------------------------------------------
//single indicates that we are only expecting one file. The string passed to single is the name of the field in the request.
app.post("/upload", uploader.single("file"), s3.upload, (req, res) => {
// console.log('req.file: ', req.file);
const url = config.s3Url + req.file.filename;
db.insertImages(
url,
req.body.username,
req.body.title,
req.body.description
)
.then(result => {
// console.log("RETURN:", result);
res.json(result.rows[0]);
})
.catch(err => {
console.log("error", err);
});
});
//-------------------------- GET, POST: /comment/:id ----------------
app.get("/comment/:id", function(req, res) {
db.getImageById(req.params.id).then(results => {
const imagesInfo = results.rows;
db.getAllComments(req.params.id).then(comments => {
imagesInfo[0].created_at = moment(imagesInfo[0].created_at,
moment.ISO_8601
).format("DD MMM YYYY, h:mma");
comments.rows.forEach(time => time.created_at = moment(time.created_at,
moment.ISO_8601
).fromNow()
);
db.getPrevAndNextId(req.params.id)
.then(prevAndNextId => {
res.json([imagesInfo, comments.rows, prevAndNextId]);
})
.catch(err => {
console.log("error", err);
});
});
});
});
app.post("/comment/:id", function(req, res) {
db.addComment(req.params.id, req.body.author, req.body.comment)
.then(comments => {
// console.log("New comment is added!!!");
// console.log("LAST:", comments);
res.json(comments.rows);
})
.catch(err => {
console.log("error", err);
});
});
//------------------------------
app.listen(8080, () => console.log("Listening!"));