-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·71 lines (54 loc) · 1.59 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
/* eslint-disable no-console */
import path from 'path';
import express from 'express';
import webpack from 'webpack';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import cookieParser from 'cookie-parser';
import passport from 'passport';
import session from 'express-session';
import api from './src/api/';
import auth from './src/auth/';
import config from './webpack.config';
import dbConfigFile from './config/config';
// const dbConfig = dbConfigFile[process.env.NODE_ENV];
const dbConfig = dbConfigFile.test;
mongoose.Promise = global.Promise;
mongoose.connect(`mongodb://${dbConfig.host}:${dbConfig.port}/${dbConfig.database}`,
(err) => { if(err) console.error(err);
console.log(' Successfully connect to MongoDB !'); });
const port = process.env.PORT || 3000;
const app = express();
const compiler = webpack(config);
app.use(bodyParser.urlencoded({
extended: true,
}));
app.use(bodyParser.json());
app.use(require('webpack-dev-middleware')(compiler, {
publicPath: config.output.publicPath,
stats: {
colors: true,
},
}));
app.use(cookieParser());
app.use(session({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use('/', auth);
app.use('/api', api);
app.use('/static', express.static(__dirname + '/public'));
app.get('*', (req, res) => {
console.log('Get user');
res.sendFile(path.join(__dirname, 'index.html'));
});
app.listen(port, (err) => {
if (err) {
console.log(err);
return;
}
console.log(`Listening at http://localhost:${port}`);
});