-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
101 lines (85 loc) · 2.42 KB
/
app.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
/**
* Application entry point.
* @copyright (c) 2016-present [email protected]
*/
import express from 'express';
import expressGraphQL from 'express-graphql';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import path from 'path';
import session from 'express-session';
import webpack from 'webpack';
import passport from 'passport';
import { Strategy } from 'passport-local';
import Router from 'Server/Router';
import { serializeUser, deserializeUser, authenticate } from 'Server/Session/handlers';
import { Schema } from 'Server/GraphQL/Schema';
import config from './config';
const PORT = process.env.PORT || 3008;
const isProduction = process.env.NODE_ENV === 'production';
/* Application */
const app = express();
const staticPath = isProduction ? path.join(__dirname, 'assets') :
path.join(__dirname, 'dist', 'assets');
/**
* Connect with mongo database.
* @method connectWithMongo
*/
function connectWithMongo() {
mongoose.connect(config.mongodb);
mongoose.Promise = Promise;
const connection = mongoose.connection;
connection.on('error', () => {
console.error('Databse connection error');
});
connection.once('open', () => {
console.log('Database connection established');
});
}
/**
* Configure application middleware.
* @method configureMiddleware
*/
function configureMiddleware() {
app.use(express.static(staticPath));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(session({
secret: 'app_secret',
cookie: { maxAge: 60000 },
resave: false,
saveUninitialized: false
}));
}
/**
* Configure passport middleware.
* @method configurePassport
*/
function configurePassport() {
passport.use(new Strategy({ session: true }, authenticate));
passport.serializeUser(serializeUser);
passport.deserializeUser(deserializeUser);
app.use(passport.initialize());
app.use(passport.session());
}
/**
* Configure graphql endpoint.
* @method configureGraphql
*/
function configureGraphql() {
app.use('/graphql', expressGraphQL({
pretty: true,
schema: Schema,
graphiql: true
}));
}
configureMiddleware();
connectWithMongo();
configurePassport();
configureGraphql();
app.use('/', Router);
app.listen(PORT, () => {
console.info(`Listen on port: ${PORT}`);
});