-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
88 lines (70 loc) · 2.55 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
// Attaching all dependencies
const express = require('express')
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const fileUpload = require('express-fileupload');
const session = require("express-session");
const passport = require('passport');
const LocalStrategy = require('passport-local');
var cookieParser = require('cookie-parser');
var flash = require('connect-flash');
// getting models
const User = require("./models/user.model")
const Music = require("./models/music.model")
// controllers
const middleware = require("./utils/middleware")
const userController = require('./controllers/user.controller')
const musicController = require('./controllers/music.controller')
const reccommendationController = require('./controllers/reccommendation.controller')
// connect mongoose
mongoose.connect("mongodb://localhost/musicApp",{ useNewUrlParser: true })
.then(() => console.log("Connected to MongoDB."))
.catch(err => console.error("Could not connect to MongoDB."));
// configure express
const app = express()
app.use("/assets/css", express.static(__dirname + "/assets/css"));
app.use("/assets/js", express.static(__dirname + "/assets/js"));
app.use("/assets/img", express.static(__dirname + "/assets/img"));
app.use("/assets/Library", express.static(__dirname + "/assets/Library"));
app.use(bodyParser.urlencoded({extended : false}));
app.use(fileUpload());
app.set("view engine", "ejs");
//passport configuration
app.use(session({
secret: process.env.SESSIONSECRET || "node_app_secret",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
// flash
app.use(cookieParser('secret'));
app.use(session({cookie: { maxAge: 60000 }}));
app.use(flash());
// pass currentUser to all routes
app.use((req, res, next) => {
res.locals.currentUser = req.user; // req.user is an authenticated user
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
});
// logout route
app.get("/logout", (req, res) => {
req.logout();
req.flash("success", "Logged out seccessfully. Look forward to seeing you again!");
res.redirect("/login");
});
// home page
// ===============================================================
// routing starts here
app.get('/',(req,res)=>{
res.redirect('/home')
})
userController(app);
musicController(app);
reccommendationController(app);
// listening to port
app.listen(3000)