-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
382 lines (320 loc) · 7.76 KB
/
app.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
const _ = require('lodash');
const sendEmail = require("./sendmail")
const utils = require("./utils/utils")
const express = require("express")
const bodyParser = require("body-parser")
const ejs = require("ejs")
const cors = require("cors")
const mongoose = require("mongoose")
const multer = require("multer")
const { log } = require("console")
const sharp = require("sharp")
const fs = require("fs")
const jwt = require("jsonwebtoken")
const app = express()
app.use(express.json())
require("dotenv").config({ path: __dirname + "/.env" })
app.set("view engine", "ejs")
app.use(
bodyParser.urlencoded({
extended: true,
})
)
app.use(express.static("public"))
app.use(
cors({
origin: [
process.env.URL1,
process.env.URL2,
process.env.URL3,
],
})
)
var imageLink = ""
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, __dirname + "/public/uploads/temp_images")
},
filename: function (req, file, cb) {
log("FILE")
log(file)
const uniqueSuffix =
Date.now() +
"" +
Math.floor(Math.random() * 1000) +
"_" +
file.originalname.toLowerCase()
cb(null, uniqueSuffix)
imageLink = uniqueSuffix
},
})
const upload = multer({ storage: storage })
mongoose
.connect(process.env.DB)
.then((msg) => {
log("Connected successesfully")
log(process.env.NODE_ENV)
})
.catch((err) => {
log("=>>>>", err)
})
const skillSchema = {
title: String,
content: String,
link: String,
icon: String,
}
const portfolioSchema = {
title: String,
content: String,
link: String,
image: String,
}
const userSchema = {
user: String,
password: String,
}
const Skill = mongoose.model("Skill", skillSchema)
const Portfolio = mongoose.model("Portfolio", portfolioSchema)
const User = mongoose.model("User", userSchema)
app
.route("/skill")
.post(authenticateToken, function (req, res) {
const skill = {
title: req.body.title,
content: req.body.content,
link: req.body.link,
icon: req.body.icon,
}
const newSkill = new Skill(skill)
newSkill.save()
res.send(skill)
})
.get((req, res) => {
Skill.find()
.then((items) => {
res.send(items)
})
.catch((err) => {
res.send(err)
})
})
//FIND ONE SKILL
app
.route("/skill/:id")
.get((req, res) => {
const id = req.params.id
Skill.findOne({ _id: id })
.then((item) => {
res.send(item)
})
.catch((err) => {
res.send("Skill doesn't Exist !")
})
})
.delete(authenticateToken, (req, res) => {
const id = req.params.id
Skill.deleteOne({ _id: id })
.then((msg) => {
if (msg.deletedCount === 1) {
res.send("Deleted successefuly !")
} else {
res.send("Can't delete")
}
})
.catch((err) => {
res.send("delete error")
})
})
.patch(authenticateToken, (req, res) => {
const id = req.params.id
const update = req.body
Skill.findByIdAndUpdate({ _id: id }, update)
.then((response) => {
res.send("UPDATED SUCCESSFULY ! THANKS")
log(response)
})
.catch((err) => {
log(err)
})
})
//SEND EMAIL
app
.route("/contact")
.post((req, res) => {
sendEmail(req.body)
log("=====================")
log(req.body)
res.send("MESSAGE RECEIVED ! THANKS")
})
//PORTFOLIO ROUTE
app
.route("/portfolio")
.post(authenticateToken, (req, res) => {
const portfolio = {
title: req.body.title,
content: req.body.content,
link: req.body.link,
image: imageLink,
}
const newPortfolioItem = new Portfolio(portfolio)
newPortfolioItem.save()
res.send(portfolio)
})
.get((req, res) => {
Portfolio.find()
.then((items) => {
res.send(items)
})
.catch((err) => {
res.send(err)
})
})
//FIND ON PORTFOLIO
app
.route("/portfolio/:id")
.get((req, res) => {
const id = req.params.id
Portfolio.findOne({ _id: id })
.then((item) => {
res.send(item)
})
.catch((err) => {
res.send("Portfolio doesn't Exist !")
})
})
.delete(authenticateToken, (req, res) => {
const id = req.params.id
Portfolio.deleteOne({ _id: id })
.then((msg) => {
if (msg.deletedCount === 1) {
res.send("Deleted successefuly !")
} else {
res.send("Can't delete")
}
})
.catch((err) => {
res.send("delete error")
})
})
.patch(authenticateToken, (req, res) => {
const id = req.params.id
var update = {
title: req.body.title,
content: req.body.content,
link: req.body.link,
}
Portfolio.findByIdAndUpdate({ _id: id }, update, { new: true })
.then((response) => {
res.send("UPDATED SUCCESSFULY ! THANKS")
})
.catch((err) => {
log(err)
})
})
//UPLOADS ROUTE
app.route("/upload")
.post(upload.single("image"),authenticateToken , (req, res) => {
const imagePath = __dirname + "/public/uploads/images/" + imageLink
const imageToDelete = __dirname + "/public/uploads/temp_images/" + imageLink
log("imagePath : ")
log(imagePath)
log("req.file.path : ")
// log()
//IF req.BODY EXIST
if (utils.isRealValue(JSON.stringify(req.body))) {
// IF AN IMAGE IS UPLOADED
if(req.file){
console.log("====>>", req.body);
Portfolio.findByIdAndUpdate(
{ _id: req.body.id_profile },
{ image: imageLink },
{ new: true }
)
.then((response) => {
log(response)
sharp(req.file.path)
.resize(640, 480)
.jpeg({
quality: 80,
cromaSubsampling: "4:4:4",
})
.toFile(imagePath, (err, info) => {
if (err) {
log(err)
} else {
fs.unlink(imageToDelete, (err, info) => {
if (err) {
log(err)
} else {
log(info)
}
})
}
})
})
.catch((err) => {
log(err)
})
log("UPDATED UPLOAD IMAGE LINK")
}
} else {
console.log("==>>", req.body);
sharp(req.file.path)
.resize(640, 480)
.jpeg({
quality: 80,
cromaSubsampling: "4:4:4",
})
.toFile(imagePath, (err, info) => {
if (err) {
log(err)
} else {
fs.unlink(imageToDelete, (err, info) => {
if (err) {
log(err)
} else {
log(info)
}
})
}
})
log("UPLOAD IMAGE")
}
log(req.body)
res.send("file uploaded !")
})
app.route("/login").post((req, res) => {
const user = { user: req.body.user, password: req.body.password }
User.exists(user)
.then((response) => {
if (response) {
const accessToken = jwt.sign(user, process.env.JWT_ACCESS_TOKEN_SECRET)
res.json({ accessToken: accessToken })
} else {
res.send("NOT EXIST")
}
})
.catch((err) => {
console.log(err)
})
})
function authenticateToken(req, res, next) {
const authHeader = req.headers["authorization"]
const token = authHeader.split(" ")[1]
if (token === null) return res.sendStatus(401)
jwt.verify(token, process.env.JWT_ACCESS_TOKEN_SECRET, (err, user) => {
if (err) return res.sendStatus(403)
req.user = user
next()
})
}
if (process.env.NODE_ENV === "prod") {
app.listen(5000, function () {
log("Server started on port 5000")
})
} else if (process.env.NODE_ENV === "dev") {
app.listen(3001, function () {
log("Server started on port 3001")
})
}