-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
51 lines (44 loc) · 1.38 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
if (process.env.NODE_ENV !== 'production') { require('dotenv').config() }
import './models';
import path from 'path';
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import basicAuth from 'express-basic-auth';
import graphqlHTTP from 'express-graphql';
import schema from './schema';
import { checkJwt } from './middlewares/auth';
import { checkIfUserExists, setupFirstUserAsCurrent } from './middlewares/currentUser';
import { setupDatabase } from './database';
setupDatabase();
const app = express();
app.use(cors());
app.use(/\/((?!graphql).)*/, bodyParser.urlencoded({ extended: true }));
app.use(/\/((?!graphql).)*/, bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(bodyParser.text({ type: 'application/graphql' }));
app.get('/', (req,res) => {
const __dirname = path.resolve();
res.sendFile(__dirname + '/index.html');
});
app.use('/graphql',
checkJwt,
checkIfUserExists,
graphqlHTTP(req => ({
schema,
context: req.fullUser,
}))
);
app.use('/graphiql',
setupFirstUserAsCurrent,
basicAuth({users: {'admin': process.env.BASIC_AUTH_PASS}, challenge: true, realm: 'Imb4T3st4pp'}),
graphqlHTTP(req => ({
schema,
graphiql: true,
context: req.fullUser,
}))
);
app.listen((process.env.PORT || 3000), () => {
console.log('+++Express Server is Running!!!');
});