-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
138 lines (122 loc) · 3.34 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
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const CONFIG = require('./config/config.json');
const log = require ('./middleware/log');
const app = express();
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const session = require('express-session');
const RedisStore = require('connect-redis')(session);
const bcrypt = require('bcrypt');
const flash = require('connect-flash');
const db = require('./models');
const Card = db.Card;
const User = db.User;
const path = require('path');
const fs = require('fs');
const webpack = require('webpack');
const webpackMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const config = require('./webpack.config.js');
const apiRouter = require('./routes/api');
const loginRouter = require('./routes/login');
const userRouter = require('./routes/users');
const priority = {
high: 'HIGH',
medium: 'MEDIUM',
low: 'LOW'
};
app.use(express.static('./public'));
app.use(bodyParser.urlencoded({ extended: true}));
app.use(flash());
app.use(session({
store: new RedisStore(),
secret: CONFIG.SECRET,
resave: false,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(log);
const ls = new LocalStrategy((username, password, done) => {
User.findAll({
attributes: ['username', 'password', 'role', 'id'],
where: {
username: username
}
}).then((user) => {
if(user.length < 1) {
return done(null, false);
}
let [u] = user;
let res = bcrypt.compareSync(password, u.dataValues.password);
if(res === true) {
return done(null, {
username: u.dataValues.username,
role: u.dataValues.role,
id: u.dataValues.id
});
} else {
return done(null, false);
}
})
.catch((err) => {
return done(null, false);
});
});
passport.serializeUser((user, done) => {
return done(null, user);
});
passport.deserializeUser((user, done) => {
return done(null, user);
});
passport.use(ls);
app.use(apiRouter);
app.use(loginRouter);
app.use(userRouter);
// Check to see what dev environment we are in
const isDeveloping = process.env.NODE_ENV !== 'production';
const port = isDeveloping ? 3000 : process.env.PORT;
if (isDeveloping) {
app.set('host', 'http://localhost');
const compiler = webpack(config);
const middleware = webpackMiddleware(compiler, {
publicPath: config.output.publicPath,
contentBase: 'src',
stats: {
colors: true,
hash: false,
timings: true,
chunks: false,
chunkModules: false,
modules: false,
},
});
const response = (req, res) => {
res.write(middleware.fileSystem.readFileSync(path.resolve(__dirname, 'dist/index.html')));
res.end();
};
app.use(middleware);
app.use(webpackHotMiddleware(compiler));
app.get('*', response);
} else {
app.use(express.static(`${__dirname}/dist`));
app.get('*', (req, res) => {
res.write(
fs.readFileSync(path.resolve(__dirname, 'dist/index.html'))
);
});
}
const onStart = (err) => {
if (err) {
throw new Error(err);
}
console.info(
`==> 🌎 Listening on port ${port}. ` +
`Open up http://localhost:${port}/ in your browser.`
);
db.sequelize.sync();
};
app.listen(port, 'localhost', onStart);
module.exports = app;