-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
175 lines (149 loc) · 4.58 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var express = require('express'),
path = require('path'),
favicon = require('serve-favicon'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
i18n = require('i18n')
var passport = require('passport'),
flash = require('express-flash'),
session = require('cookie-session')
// jsonParser = bodyParser.json();
var RedisServer = require('redis-server')
// Simply pass the port that you want a Redis server to listen on.
var server = new RedisServer(6379)
server.open(err => {
if (err === null) {
console.log('redis server connected')
// You may now connect a client to the Redis
// server bound to `server.port` (e.g. 6379).
}
})
var app = express()
function requireHTTPS(req, res, next) {
if((!req.secure) && (req.get('X-Forwarded-Proto') !== 'https')) {
//FYI this should work for local development as well
return res.redirect('https://' + req.get('host') + req.url)
} else {
next()
}
}
if ('production' == app.get('env')) {
// just for production code
app.use(requireHTTPS);
}
if ('development' == app.get('env')) {
// just for development code
console.info('setting dev server...')
var webpack = require('webpack'),
webpackDevMiddleware = require('webpack-dev-middleware'),
webpackHotMiddleware = require('webpack-hot-middleware'),
webpackConfig = require('./webpack.config')
var compiler = webpack(webpackConfig)
app.use(
webpackDevMiddleware(compiler, {
hot: true,
filename: 'bundle.js',
publicPath: webpackConfig.output.publicPath,
noInfo: true,
stats: {
colors: true,
},
historyApiFallback: true,
})
)
app.use(
webpackHotMiddleware(compiler, {
log: console.log,
path: '/__webpack_hmr',
heartbeat: 10 * 1000,
})
)
}
// view engine setup
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'jade')
// uncomment after placing your favicon in /public
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
app.use(logger('dev'))
app.use(bodyParser.json({ limit: '700kb' }))
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(
session({
secret: '4564f6s4fdsfdfd',
resave: true,
saveUninitialized: true,
})
)
app.use(express.static(path.join(__dirname, 'public')))
app.use(passport.initialize())
app.use(passport.session())
app.use(flash())
// app.use(logger('dev'));
// app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({ extended: false }));
// app.use(cookieParser());
//--------User-Auth----------
// app.use(session({ secret: '4564f6s4fdsfdfd', resave: false, saveUninitialized: false }))
app.use(function(req, res, next) {
res.locals.errorMessage = req.flash('error')
next()
})
var Strategies = require('./app/controllers/signupController')
passport.use('signup', Strategies.SignUpStrategy)
passport.use('login', Strategies.LoginStrategy)
passport.use('facebookLogin', Strategies.FacebookLoginStrategy)
passport.use('googleLogin', Strategies.GoogleLoginStrategy)
app.use(express.static(path.join(__dirname, 'public')))
// Routes
var appRouter = require('./app/routers/appRouter')
var users = require('./app/routers/users')
app.use('/users', users)
app.use('/', appRouter)
// app.use('/datajson', datajson);
// language packages
i18n.configure({
locales: ['en', 'ar'],
directory: __dirname + '/locales',
})
app.use(i18n.init)
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found')
err.status = 404
next(err)
})
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res) {
res.status(err.status || 500)
console.log(err)
res.json({
message: err.message,
error: err,
})
})
}
// production error handler
// no stacktraces leaked to user
if (app.get('env') === 'production') {
app.use(function(err, req, res) {
res.status(err.status || 500)
res.json({
message: err.message,
error: {},
})
})
}
// app.enable('trust proxy')
// var forceSsl = function (req, res, next) {
// if (req.headers['x-forwarded-proto'] !== 'https') {
// return res.redirect(['https://', req.get('Host'), req.url].join(''))
// }
// return next()
// }
// app.use(forceSsl)
module.exports = app