-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
61 lines (51 loc) · 1.89 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
if (process.env.NODE_ENV === 'development') {
require('dotenv').config();
}
const express = require('express');
const passport = require('passport');
const helmet = require('helmet');
const compression = require('compression');
const ms = require('ms');
const fs = require('fs');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
const { logger } = require('./server/config/logger');
const { mongoose } = require('./server/db');
require('./server/auth/auth.controller').configPassport(passport);
const app = express();
const port = process.env.PORT || 3000;
const appDirectory = path.join(__dirname, 'client/dist');
const libDirectory = path.join(__dirname, 'client/bower_components');
const staticOptions = {maxAge: ms('1y')};
app.use(compression());
app.use(helmet());
app.use(express.static(appDirectory, staticOptions));
app.use(express.static(libDirectory, staticOptions));
app.use(cookieParser());
// parse request body and add to req.body
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
secure: true,
store: new MongoStore({mongooseConnection: mongoose.connection})
}));
app.use(passport.initialize());
app.use(passport.session());
// import routes
require('./server/auth/auth.routes')(app, passport);
require('./server/routes/data')(app);
require('./server/routes/diary')(app);
require('./server/routes/error')(app);
require('./server/routes/config')(app);
app.listen(port, () => {
logger.log('info', `SERVER PORT: ${port}`);
logger.log('info', `SERVING APP FROM: ${appDirectory}`);
logger.log('info', `SERVING LIB FROM: ${libDirectory}`);
logger.log('info', `NODE_ENV: ${process.env.NODE_ENV}`);
});