-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
89 lines (77 loc) · 2.19 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* eslint-disable no-console */
// imports
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const jwt = require('jsonwebtoken');
const path = require('path');
// bring in the graphql middleware
const { graphiqlExpress, graphqlExpress } = require('apollo-server-express');
const { makeExecutableSchema } = require('graphql-tools');
// local imports
require('dotenv').config({ path: 'variables.env' });
const Recipe = require('./models/Recipes');
const User = require('./models/User');
const { resolvers } = require('./resolvers');
const { typeDefs } = require('./schema');
// make executable graphql schema
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
// connecting to the database
mongoose
.connect(process.env.MONGO_URI)
.then(() => console.log('DB Connected'))
.catch(err => console.log('The error is: ', err));
// running our express application
const app = express();
const corsOptions = {
origin: 'https://gentle-hollows-45761.herokuapp.com',
credentials: true,
};
app.use(cors(corsOptions));
// set up JWT authentication middleware
app.use(async (req, res, next) => {
const token = req.headers.authorization;
if (token) {
try {
const currentUser = await jwt.verify(token, process.env.SECRET);
req.currentUser = currentUser;
} catch (err) {
console.log(err);
}
}
next();
});
// setting up graphiql application
app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));
// connect schema to graphql
app.use(
'/graphql',
bodyParser.json(),
graphqlExpress(({ currentUser }) => ({
introspection: true,
playground: true,
schema,
context: {
Recipe,
User,
currentUser,
},
}))
);
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
// using environmental vairable port or the default port
const { PORT } = process.env;
// const PORT = process.env.PORT || 4444;
// listening to port changes
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});