-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathserver.js
99 lines (87 loc) · 2.99 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
if (process.env.NODE_ENV !== 'production') {
require('babel-register')({ ignore: /node_modules/ })
}
const fs = require('fs')
if (!fs.existsSync('./config.json')) {
throw Error(
'You need to create a config.json file in the root directorly with the required environment variables. You can use the config.sample.json file as a template.'
)
}
// React
const React = require('react')
const ReactDOMServer = require('react-dom/server')
const { StaticRouter } = require('react-router')
const { Provider } = require('react-redux')
const { store } =
process.env.NODE_ENV === 'production'
? require('./public/production/client/redux/store')
: require('./client/redux/store')
const Routes =
process.env.NODE_ENV === 'production'
? require('./public/production/client/components/Router/CompiledRoutes')
.default
: require('./client/components/Router/CompiledRoutes').default
const Layout =
process.env.NODE_ENV === 'production'
? require('./public/production/client/components/Layout').default
: require('./client/components/Layout').default
// Template for injecting server-side rendered React markup
const _template = require('lodash/template')
const baseTemplate = fs.readFileSync('./index.html')
const template = _template(baseTemplate)
// App configuration and secrets
const config = require('./config.json')
const connectMongoose = require('./server/db/connectMongoose')
const MONGO_URI =
process.env.NODE_ENV === 'production'
? config.mongoUriProduction
: config.mongoUriDev
const express = require('express')
const helmet = require('helmet')
const bodyParser = require('body-parser')
const passport = require('passport')
const localStrategy = require('./server/passport/localStrategy')
const githubStrategy = require('./server/passport/githubStrategy')
const PORT = process.env.PORT || config.expressPort
const app = express()
const signup = require('./server/routes/signup')
const login = require('./server/routes/login')
const user = require('./server/routes/user')
connectMongoose(MONGO_URI)
// Middleware
app.use(helmet())
app.use(bodyParser.json())
app.use(passport.initialize())
passport.use('local-login', localStrategy)
passport.use('login-github', githubStrategy)
// Routes
app.use('/api/signup', signup)
app.use('/api/login', login)
app.use('/api/user', user)
app.use('/public', express.static('./public'))
/**
* Handles server requests, and serves per-rendered context-aware
* React markup based on the url
*/
app.use((req, res) => {
const context = {}
const body = ReactDOMServer.renderToString(
React.createElement(
Provider,
{ store },
React.createElement(
StaticRouter,
{ location: req.url, context: context },
React.createElement(Layout, null, React.createElement(Routes))
)
)
)
if (context.url) {
res.redirect(context.url)
} else {
res.status(200).send(template({ body }))
}
})
console.log('node environment:', process.env.NODE_ENV)
console.log(`express: Listening on port ${PORT}`)
app.listen(PORT)