-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
110 lines (89 loc) · 2.46 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
const express = require("express");
const ConnectDB = require("./db/connection");
const app = express();
const expressSession = require('express-session');
const passport = require("passport");
const port = process.env.port || 5000;
const register = require('./auth/auth');
const { PassInit, isAuthenticted } = require('./auth/passportConf');
const fs = require('fs');
const path = require('path');
const { upload } = require('./middleware/imageHandler');
const File = require('./models/file');
PassInit(passport);
// Middlwere Usages
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(expressSession({
secret: 'secret',
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7
}
}));
app.use(passport.initialize());
app.use(passport.session());
app.set("view engine", "ejs");
//Server Initial Setup
ConnectDB();
app.listen(port, () => {
console.log(`website is runnig at : http://localhost:${port}`);
});
// Get Request Handeling
app.get("/", (req, res) => {
res.render("home");
});
app.get("/register", (req, res) => {
res.status(200).render("register");
});
app.get("/login", (req, res) => {
res.status(200).render("login");
});
app.get('/secret', isAuthenticted, (req, res) => {
res.render('secret');
})
app.get('/logout', (req, res) => {
req.logout(() => {
res.redirect('/');
});
});
app.get('/photos',async(req,res)=>{
const imgs = await File.find({}).then((data,err)=>{
if(err){
console.log(err);
}
res.status(200).render('photos',{imgs:data})
})
})
app.get('/test',(req,res)=>{
res.status(200).render('test');
})
app.get('/get-img',async(req,res)=>{
const imgs = await File.find({}).then((data,err)=>{
if(err){
console.log(err);
}
res.status(200).send(data);
})
})
// Post Requests Handeling
app.post('/data/img',upload.single('avatar'), async (req, res) => {
const obj = {
img: {
data: fs.readFileSync(path.join(__dirname + '/uploads/' + req.file.filename)),
contentType: 'image/png'
}
}
await File.create(obj).then((data) => {
res.status(200).json(data);
}
).catch((err) => {
res.status(400).json({ message: "Error" });
}
)
});
app.post('/api/register', register);
app.post('/api/login', passport.authenticate('local'), (req, res) => {
res.status(200).json({ message: "Succsess" });
});