-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
94 lines (72 loc) · 3.12 KB
/
server.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
// dotenv is used to load environment variables from .env file
require('dotenv').config()
const express = require('express')
const MongoStore = require('connect-mongo')
const app = express()
const morgan = require('morgan')
// Importing the fs and https modules -------------- STEP 1
const https = require("https");
const fs = require("fs");
// Read the certificate and the private key for the https server options
// ------------------- STEP 2
const options = {
key: fs.readFileSync("./config/cert.key"),
cert: fs.readFileSync("./config/cert.crt"),
};
// mongoose used for connection to mongodb database using mongoose module
const mongoose = require('mongoose')
mongoose.connect(process.env.DATABASE_URL)
const db = mongoose.connection
db.on('error', (error) => console.error(error))
db.once('open', () => console.log('Connected to Database'))
const ejs = require('ejs')
app.set('view engine', 'ejs')
app.use(express.static('public'))
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
const fileUpload = require('express-fileupload')
app.use(fileUpload())
const expressSession = require('express-session')
app.use(expressSession({
secret: 'keyboard cat',
proxy: true,
resave: true,
saveUninitialized: true,
store: MongoStore.create({ mongoUrl: process.env.DATABASE_URL })
}))
// logging middleware
app.use(morgan('combined'));
global.loggedIn = null;
app.use("*", (req, res, next) => {
loggedIn = req.session.userId;
next();
});
const homePageController = require('./controllers/homePage')
const searchController = require('./controllers/search')
const uploadVideoController = require('./controllers/uploadVideo')
const storeVideoController = require('./controllers/storeVideo')
const getVideoController = require('./controllers/getVideo')
const signUpController = require('./controllers/signUp')
const storeUserController = require('./controllers/storeUser')
const loginController = require('./controllers/login')
const loginUserController = require('./controllers/loginUser')
const authMiddleware = require('./middleware/authMiddleware')
const redirectIfAuthenticatedMiddleware = require('./middleware/redirectIfAuthenticatedMiddleware')
const logoutController = require('./controllers/logout')
app.get('/', homePageController)
app.post('/search', searchController)
app.get('/uploadVideo', authMiddleware, uploadVideoController)
app.post('/post/video', authMiddleware, storeVideoController)
app.get('/:slug', getVideoController)
app.get('/auth/signUp', redirectIfAuthenticatedMiddleware, signUpController)
app.post('/user/signup', redirectIfAuthenticatedMiddleware, storeUserController)
app.get('/auth/login', redirectIfAuthenticatedMiddleware, loginController)
app.post('/user/login', redirectIfAuthenticatedMiddleware, loginUserController)
app.get('/auth/logout', logoutController)
app.use((req, res) => res.render('notFound'))
// Create the https server by initializing it with 'options'
// -------------------- STEP 3
https.createServer(options, app).listen(process.env.PORT, () => {
console.log(`HTTPS server started on port ${process.env.PORT}`);
});