-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (59 loc) · 1.71 KB
/
index.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
// Include .env variables
require('dotenv').config();
let apiCallUpperBound = 151;
// Require necessary modules
let express = require('express');
let flash = require('connect-flash');
var request = require('request');
let layouts = require('express-ejs-layouts');
let session = require('express-session');
var methodOverride = require("method-override");
let userTable = require('./models/user.js')
let db = require('./models');
// Include passport configuration
let passport = require('./config/passportConfig');
// Declare Express app
let app = express();
// Set view engine
app.set('view engine', 'ejs');
// Include (use) middleware
app.use('/', express.static('public'));
app.use(layouts);
app.use(express.urlencoded({ extended: false }));
app.use(methodOverride("_method"));
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
// Custom middleware - write data to locals on EVERY page
app.use((req, res, next) => {
res.locals.alerts = req.flash();
res.locals.user = req.user;
next();
});
// Include routes from controllers
app.use('/auth', require('./controllers/auth'));
app.use('/profile', require('./controllers/profile'));
app.use('/search', require('./controllers/search'));
// Make a home route: GET /
app.get('/', (req, res) => {
let existsUser = false;
if (req.user) {
existsUser = true;
}
res.render('home', {
existsUser: existsUser
});
})
// Catch-all route - render the 404 page
app.get('*', (req, res) => {
res.render('404');
})
// Listen from your port
app.listen(process.env.PORT || 3013, () => {
console.log(`listening on ${process.env.PORT || 3013}`);
});