-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
88 lines (70 loc) · 2.4 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
import webpack from 'webpack';
import webpackMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import express from 'express';
import path from 'path';
import http from 'http';
import bodyParser from 'body-parser';
import webpackConfig from './webpack.config';
import jwt from 'jsonwebtoken';
import jwtConfig from './jwt.config.json';
const isProduction = process.env.NODE_ENV === 'production';
const isDeveloping = !isProduction;
const app = express();
// Webpack dev server
if (isDeveloping) {
const WEBPACK_PORT = 3001;
const compiler = webpack(webpackConfig);
app.use(webpackMiddleware(compiler, {
publicPath: webpackConfig.output.publicPath,
stats: {
colors: true,
hash: false,
timings: true,
chunks: false,
chunkModules: false,
modules: false
}
}));
app.use(webpackHotMiddleware(compiler));
app.listen(WEBPACK_PORT, 'localhost', function (err, result) {
if (err) {
console.log(err);
}
console.log('WebpackDevServer listening at localhost:'+WEBPACK_PORT);
});
}
// RESTful API
const publicPath = path.resolve(__dirname);
app.use(bodyParser.json({ type: 'application/json' }))
app.use(express.static(publicPath));
const port = isProduction ? (process.env.PORT || 80) : 3000;
// this is necessary to handle URL correctly since client uses Browser History
app.get('*', function (request, response){
response.sendFile(path.resolve(__dirname, '', 'index.html'))
})
app.post('/api/login', function(req, res) {
const credentials = req.body;
if(credentials.user==='admin' && credentials.password==='password'){
const profile = {'user': credentials.user, 'role': 'ADMIN'};
const jwtToken = jwt.sign(profile, jwtConfig.secret, {'expiresIn' : 5*60}); // expires in 300 seconds (5 min)
res.status(200).json({
id_token: jwtToken
});
//res.json({'user': credentials.user, 'role': 'ADMIN'});
}else{
res.status(401).json({'message' : 'Invalid user/password'});
}
});
app.post('/api/logout', function(req, res) {
res.status(200).json({'message' : 'User logged out'});
});
// We need to use basic HTTP service to proxy
// websocket requests from webpack
const server = http.createServer(app);
server.listen(port, function (err, result) {
if(err){
console.log(err);
}
console.log('Server running on port ' + port);
});