-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
116 lines (100 loc) · 3.43 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var express = require('express');
var compression = require('compression');
var app = express();
var helmet = require('helmet');
app.use(helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'none'"],
manifestSrc: ["'self'"],
connectSrc: ["'self'", 'https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/'],
styleSrc: ["'self'", 'maxcdn.bootstrapcdn.com', 'fonts.googleapis.com'],
fontSrc: ["'self'", 'fonts.gstatic.com', 'maxcdn.bootstrapcdn.com'],
scriptSrc: ["'self'",
'https://storage.googleapis.com/workbox-cdn/releases/3.2.0/workbox-sw.js',
'https://storage.googleapis.com/workbox-cdn/releases/3.2.0/workbox-routing.prod.js',
'https://storage.googleapis.com/workbox-cdn/releases/3.2.0/workbox-core.prod.js',
'https://storage.googleapis.com/workbox-cdn/releases/3.2.0/workbox-strategies.prod.js',
'https://storage.googleapis.com/workbox-cdn/releases/3.2.0/workbox-cache-expiration.prod.js'],
imgSrc: ["'self'", 'pbs.twimg.com'],
frameAncestors: ["'none'"],
baseUri: ["'none'"],
formAction: ["'self'"]
}
}));
app.use(helmet.referrerPolicy({ policy: 'same-origin' }))
app.use(compression());
app.use(helmet());
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
var passport = require('passport'),
TwitterStrategy = require('passport-twitter').Strategy;
var session = require('express-session');
var mongoose = require('mongoose');
var MongoStore = require('connect-mongo')(session);
mongoose.connect(process.env.MONGODB_URL);
if (process.env.NODE_ENV === 'production') {
app.use(session({
proxy: true,
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: { secure: true },
store: new MongoStore({
mongooseConnection: mongoose.connection,
touchAfter: 24 * 3600 // time period in seconds
})
}));
}
else {
app.use(session({
proxy: true,
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store: new MongoStore({
mongooseConnection: mongoose.connection,
touchAfter: 24 * 3600 // time period in seconds
})
}));
}
// Initialize Passport and restore authentication state, if any, from the
// session.
app.use(passport.initialize());
app.use(passport.session());
// set the view engine to ejs
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/views'));
var login = require('./login.js')
login.twitterAuth(app);
login.authCallback(app);
var fetchTweets = require('./fetchTweets.js');
fetchTweets.fetchTweets(app);
var classifyUserName = require('./classifyUserName');
classifyUserName.classifyUserName(app);
var extractData = require('./extractUserData');
extractData.extractUserData(app, bodyParser);
var logout = require('./logout');
logout.logoutUser(app);
//redirect http traffic to https if app is in production environment
app.use(function(req, res, next) {
if(process.env.NODE_ENV === 'production' && req.headers['x-forwarded-proto']!='https') {
return res.redirect(['https://', req.get('Host'), req.url].join(''));
}
next();
});
//index page
app.get('/', (request, response) => {
if (request.user != null) {
response.render('index.ejs', {
userName: request.user.userName
});
} else {
response.render('index', {
userName: ''
});
}
});
var server_port = process.env.PORT || 8085;
app.listen(server_port, function () {
console.log("Listening on " + server_port);
});