-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
117 lines (80 loc) · 3.03 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
117
require('dotenv').config()
require('babel-core/register');
process.env.ROOT = __dirname;
var path = require('path');
var webpack = require('webpack');
var express = require('express');
var config = require('./webpack.config');
var bodyParser = require('body-parser');
const passport = require('passport')
const session = require('express-session')
const RedisStore = require('connect-redis')(session)
var cookieParser = require('cookie-parser')
var cors = require("cors");
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
var app = express();
var compiler = webpack(config);
require('./authentication').init(app);
// ------------------------------------------------------------ MIDDLEWARE ---------------------------------------------------------------------
app.use(cookieParser());
app.use(cors({origin: "https://photo-app-zeta.herokuapp.com/"}));
app.use(session({
store: new RedisStore({
url: process.env.REDIS
}),
secret: process.env.SECRET,
resave: false,
saveUninitialized: false,
cookie: { maxAge: 600000 }
}));
app.use(passport.initialize())
app.use(passport.session())
app.use(express.static(path.join(__dirname, 'public')));
app.use(require('webpack-dev-middleware')(compiler, {
publicPath: config.output.publicPath,
noInfo: true
}));
app.use(require('webpack-hot-middleware')(compiler));
app.use( bodyParser.json() );
app.use(bodyParser.urlencoded({
extended: true
}));
app.enable('trust proxy');
app.use(function(request, response, next){
var okay_route = ["/my_pics", "/my_pics/"];
if (request.isAuthenticated() && okay_route.indexOf(request.path) != -1){
response.redirect("/user?id=" + request.user._id);
} else if (request.isAuthenticated() == false && okay_route.indexOf(request.path) != -1){
response.redirect("/home");
} else {
return next();
}
});
app.use(function(request, response, next){
var okay_route = ["/first", "/first/", "/logout/", "/logout", "/set_disp", "/set_disp/"];
if (request.isAuthenticated() && request.user.firstTime && okay_route.indexOf(request.path) == -1){
response.redirect("/first");
} else {
return next();
}
});
// ------------------------------------------------------------ ROUTES ---------------------------------------------------------------------------
var routes = require('./routes');
routes(app);
// ------------------------------------------------------------ SERVER LISTEN ---------------------------------------------------------------------------
//app.listen(3000, '0.0.0.0', function(err) {
app.listen(process.env.PORT || 3000, function(err) {
if (err) {
return console.error(err);
}
console.log('Listening at ' + process.env.BASE_URL);
});
// ------------------------------------------------------------ MISC FUNCTIONS ---------------------------------------------------------------------------
function safeStringify(obj) {
return JSON.stringify(obj)
.replace(/<\/(script)/ig, '<\\/$1')
.replace(/<!--/g, '<\\!--')
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029');
}