-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
125 lines (98 loc) · 2.89 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
require("dotenv").config();
const express = require("express");
const ejs = require('ejs');
const app = express();
const mongoose = require("mongoose");
const userRoutes = require("./routes/users");
const authRoutes = require("./routes/auth");
const auth = require("./middleware/auth");
const bodyParser = require("body-parser")
const session = require('express-session');
const { User, validate } = require("./models/user");
const mongoDbSession = require('connect-mongodb-session')(session)
//middlewares
app.use(express.static('static'));
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.json());
//set view engine
app.set('view engine', 'ejs');
//connecting to mongodb
const dburl = process.env.MongoURI;
mongoose.connect(dburl, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('DB connected successfully...'))
.catch((err) => console.log('DB could not connect!\nError: ',err));
const store = new mongoDbSession({
uri: dburl,
collection: "sessions"
})
app.use(session({
secret: process.env.JWTSECRET,
resave: false,
saveUninitialized: false,
store: store
}))
//Authentication
const isAuth = function(req, res, next) {
if (req.session.isAuth) {
next()
} else {
req.session.error = '';
res.render('login', {
isAuth: req.session.isAuth,
message: "You are not logged in!",
title: "Log In | "
})
}
}
app.post('/addliked', async(req,res)=>{
likedSong=req.body.songC;
const user = await User.findByIdAndUpdate(req.session.user_id,{$push:{"likedSongs":likedSong}});
})
app.post('/rmvliked', async (req,res)=>{
removedSong = req.body.songC;
const user = await User.findByIdAndUpdate(req.session.user_id, {$pull: { "likedSongs": removedSong }});
})
app.get('/', isAuth,(req, res) => {
res.render('home.ejs',{
title: ''
})
});
app.use("/api/users/", userRoutes);
app.use("/api/login/", authRoutes);
app.get('/queue',auth,(req,res)=>{
res.render('queue',{
isAuth:false,
title:"queue |",
list: []
})
})
app.get('/player',isAuth,(req,res)=>{
res.render('player',{
isAuth:false,
title:"queue |"
})
})
app.get('/dashboard',isAuth,(req,res)=>{
res.render('dashboard',{
isAuth:true,
title:"queue |"
})
})
app.get('/seeall/:id',isAuth,async (req,res)=>{
const user = await User.findById(req.session.user_id);
// console.log(user.likedSongs);
res.render('seeall',{
isAuth:false,
title:"All songs |",
id: req.params.id,
lkSongs : user.likedSongs
})
})
app.get('/logout', function(req, res) {
req.session.destroy((err) => {
if (err) throw err;
res.redirect('/')
})
})
const PORT = process.env.PORT || 8000 ;
app.listen(PORT,console.log(`Server running on http://localhost:${PORT}/`));