-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
133 lines (103 loc) · 2.92 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
var express = require('express')
var path = require('path')
var app = express()
require('jsx-hook')()
var routes = require('./routes/index')
var bodyParser = require('body-parser')
var passport = require('passport')
var LocalStrategy = require('passport-local').Strategy
var session = require('express-session')
var React = require('react')
var Router = require('react-router')
var match = Router.match
var RouterContext = Router.RouterContext
var ReactDOMServer = require('react-dom/server')
var clientRoutes = require('./app/routes.jsx')
var Root = require('baobab-react/wrappers').Root
var Tree = require('./app/tree')
var PRODUCTION = 'production' === process.env.NODE_ENV
var user = {
id: 1,
username: 'hunter33',
password: 'password1'
}
app.use(express.static(PRODUCTION ? 'public' : 'app'))
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'jade')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: false
}))
app.use(session({
secret: 'secret',
resave: false,
saveUnitialized: false
}))
app.use(passport.initialize())
app.use(passport.session())
passport.use(new LocalStrategy({
usernameField: 'username',
passwordField: 'password'
}, function(username, password, done) {
if (username === user.username && password === user.password) {
return done(null, user)
} else {
return done(null, false, {
message: 'Username and password do not match'
})
}
}))
passport.serializeUser(function(user, done) {
done(null, user.id)
})
passport.deserializeUser(function(id, done) {
if (user.id === id) {
done(null, user)
} else {
done('Wrong id')
}
})
app.get('/getData', function(req, res) {
var fakeData = 'Hello From servet to client'
res.json({
data: fakeData
})
})
app.use('/', routes)
function safeStringify(object) {
// there are tricky rules about safely embedding JSON within HTML
return JSON.stringify(object)
.replace(/</g, '\\u003c')
.replace(/-->/g, '--\\>')
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029')
}
app.get('*', function(req, res) {
match({
routes: clientRoutes,
location: req.url
}, function(error, redirectLocation, renderProps) {
if (error) {
res.status(500).send(error.message)
} else if (redirectLocation) {
res.redirect(302, redirectLocation.pathname + redirectLocation.search)
} else if (renderProps) {
var tree = Tree(res.locals.tree)
var props = {
tree: tree
}
var page = ReactDOMServer.renderToString(React.createElement(Root, props, React.createElement(RouterContext, renderProps)))
res.render('layout', {
page: page,
appTree: 'window._BAOBAB_TREE_=' + safeStringify(props),
isProduction: PRODUCTION
})
} else {
res.status(404).send('Not Found')
}
})
})
var port = process.env.PORT || '3000'
app.listen(port, function() {
console.log('Server listen at *:' + port)
})