-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
65 lines (55 loc) · 1.96 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
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const graphqlHttp = require('express-graphql');
const knexLogger = require('knex-logger');
const morgan = require('morgan');
const db = require('./knex');
const cors = require('cors');
const compression = require('compression');
// importing the graphql schema and resolver functions.
const graphQlSchema = require('./graphql/schema/index');
const graphQlResolvers = require('./graphql/resolvers/index');
// express server.
const app = express();
app.use(cors());
// Compress all HTTP responses
app.use(compression());
// logging.
app.use(knexLogger(db));
app.use(morgan('dev'));
// body parser.
app.use(bodyParser.json());
// serves static build files
// uses pug to serve maintenace page if the maintenance mode is turned on.
if (process.env.MAINTENANCE === 'true') {
app.use('/style', express.static('maintenance/style'));
app.use('/image', express.static('maintenance/img'));
app.set('views', './maintenance/views');
app.set('view engine', 'pug');
} else {
app.use(express.static(path.join(__dirname, 'client/build')));
}
// setting up the graphql end points.
// passing in the graphql schema and resolver functions.
app.use('/graphql',
graphqlHttp({
schema: graphQlSchema,
rootValue: graphQlResolvers,
graphiql: true
}));
// renders react files if request doesn't go to api
app.get('/*', (req, res) => {
// serves maintenance static page if the maintenance mode is turned on.
if (process.env.MAINTENANCE === 'true') {
res.render('maintenance', {start: process.env.MAINTENANCE_START, end: process.env.MAINTENANCE_END});
} else {
res.sendFile('index.html', { root: './client/build' });
}
});
// use port no. 5000 for server if environment variable is not present.
const port = process.env.PORT || 5000;
const server = app.listen(port, () => {
console.log('Server Started');
});
module.exports = server;