-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
175 lines (155 loc) · 5.49 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
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')
var path = require('path')
var logger = require('morgan')
var compression = require('compression')
var cookieParser = require('cookie-parser')
var bodyParser = require('body-parser')
var expressValidator = require('express-validator')
var dotenv = require('dotenv')
var React = require('react')
var ReactDOM = require('react-dom/server')
var Router = require('react-router')
var Provider = require('react-redux').Provider
var mongoose = require('mongoose')
var jwt = require('jsonwebtoken')
var sass = require('node-sass-middleware')
var webpack = require('webpack')
var config = require('./webpack.config')
const favicon = require('serve-favicon')
// Load environment variables from .env file
dotenv.load()
// ES6 Transpiler
require('babel-core/register')
require('babel-polyfill')
// Models
var User = require('./models/User')
// Controllers
var authController = require('./controllers/auth')
var contactController = require('./controllers/contact')
// React and Server-Side Rendering
var routes = require('./app/routes')
var configureStore = require('./app/store/configureStore').default
var app = express()
var compiler = webpack(config)
mongoose.Promise = global.Promise
mongoose.connect(process.env.MONGODB, { useMongoClient: true })
mongoose.connection.on('error', function () {
console.log('MongoDB Connection Error. Please make sure that MongoDB is running.')
process.exit(1)
})
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'jade')
app.set('port', process.env.PORT || 3000)
app.use(compression())
app.use(sass({ src: path.join(__dirname, 'public'), dest: path.join(__dirname, 'public') }))
app.use(logger(app.get('env') !== 'production' ? 'dev' : 'common'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(expressValidator())
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
app.use((req, res, next) => {
res.setHeader('X-Clacks-Overhead', 'GNU Terry Pratchett')
next()
})
app.use(function (req, res, next) {
req.isAuthenticated = function () {
var token = (req.headers.authorization && req.headers.authorization.split(' ')[1]) || req.cookies.token
try {
return jwt.verify(token, process.env.TOKEN_SECRET)
} catch (err) {
return false
}
}
if (req.isAuthenticated()) {
var payload = req.isAuthenticated()
User.findById(payload.sub)
.populate('roles')
.then((user) => {
if (user) req.user = user.toObject({ virtuals: true })
next()
}, next)
.catch(next)
} else {
next()
}
})
if (app.get('env') === 'development' || !app.get('env')) {
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath
}))
app.use(require('webpack-hot-middleware')(compiler))
}
const apis = {
genre: require('./controllers/genre'),
drink: require('./controllers/drink'),
game: require('./controllers/game'),
user: require('./controllers/user'),
role: require('./controllers/role'),
asset: require('./controllers/asset')
}
app.post('/contact', contactController.contactPost)
app.put('/account', authController.ensureAuthenticated, authController.accountPut)
app.delete('/account', authController.ensureAuthenticated, authController.accountDelete)
app.post('/signup', authController.signupPost)
app.post('/login', authController.loginPost)
app.post('/forgot', authController.forgotPost)
app.post('/reset/:token', authController.resetPost)
app.get('/unlink/:provider', authController.ensureAuthenticated, authController.unlink)
app.post('/auth/facebook', authController.authFacebook)
app.get('/auth/facebook/callback', authController.authFacebookCallback)
app.post('/auth/google', authController.authGoogle)
app.get('/auth/google/callback', authController.authGoogleCallback)
app.post('/auth/twitter', authController.authTwitter)
app.get('/auth/twitter/callback', authController.authTwitterCallback)
app.use('/api/genre', apis.genre)
app.use('/api/drink', apis.drink)
app.use('/api/game', apis.game)
app.use('/api/user', apis.user)
app.use('/api/role', apis.role)
app.use('/api/asset', apis.asset)
app.use('/api/teapot', (req, res) => { res.status(418).send({ msg: 'I\'m a teapot' }) })
// React server rendering
app.use(function (req, res) {
var initialState = {
auth: { token: req.cookies.token, user: req.user },
messages: {},
styles: {
mainColour: '#ffffff',
mainBg: '#2196f3',
bg: '#ffffff'
}
}
var store = configureStore(initialState)
Router.match({ routes: routes.default(store), location: req.url }, function (err, redirectLocation, renderProps) {
if (err) {
res.status(500).send(err.message)
} else if (redirectLocation) {
res.status(302).redirect(redirectLocation.pathname + redirectLocation.search)
} else if (renderProps) {
var html = ReactDOM.renderToString(React.createElement(Provider, { store: store },
React.createElement(Router.RouterContext, renderProps)
))
res.render('layout', {
html: html,
initialState: store.getState(),
title: 'Manapot'
})
} else {
res.sendStatus(404)
}
})
})
// Production error handler
if (app.get('env') === 'production') {
app.use(function (err, req, res, next) {
console.error(err.stack)
res.sendStatus(err.status || 500)
})
}
app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'))
})
module.exports = app