-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
58 lines (50 loc) · 1.83 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
'use strict';
const dotenv = require('dotenv').config();
if (dotenv.error) {
throw dotenv.error
}
const express = require('express');
const app = express();
const session = require('express-session');
const server = require('http').Server(app);
const path = require('path');
const passport = require('passport');
const bodyParser = require('body-parser');
const compression = require('compression');
const watchlist = require(path.join(__dirname, 'routes/watchlist.route'));
const auth = require(path.join(__dirname, 'routes/auth.route'));
const PORT = process.env.PORT || 3000;
// MIDDLEWARE
app.use(bodyParser.urlencoded({ extended: true }));
app.use(compression());
app.set('views', path.join(__dirname, 'views'));
app.use('/public', express.static(path.join(__dirname, 'public'), { maxAge: 31557600000 }));
app.set('view engine', 'pug');
app.use(session({
secret: 'mowe18',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
app.use('/auth', auth);
app.use((req, res, next) => {
if (req.user) {
next();
} else {
if (req.headers.accept.match(/(text\/html|application\/xhtml\+xml)/) !== null) res.status(401).render('auth/login', { title: 'Login', reqUrl: req.url, mainClass: 'login', login: req.user });
else if (req.headers.accept.match(/application\/json/) !== null) res.status(401).json( { message: 'Unauthorized' } );
else res.status(401).send('Unauthorized');
}
});
app.use('/watchlist', watchlist);
// INDEX
app.get('/', (req, res) => {
res.render('index', { title: 'Watchlist', mainClass: 'index', login: req.user }, (err, html) => {
res.send(html);
});
});
app.use((req, res) => {
res.status(404).render('404', { title: '404 - Not found', mainClass: 'error404', login: req.user });
});
server.listen(PORT, () => console.log(`Server runs on port: ${PORT}`));