-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
99 lines (83 loc) · 2.67 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
90
91
92
93
94
95
96
97
98
99
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const path = require('path');
const cors = require('cors');
require('dotenv').config({ path: 'variables.env' });
const jwt = require('jsonwebtoken');
// Import models
const Car = require('./models/Car');
const User = require('./models/User');
// GraphQL-Express Middlewre
//const { graphiqlExpress, graphqlExpress } = require('apollo-server-express');
const { graphqlExpress } = require('apollo-server-express');
const expressPlayground = require('graphql-playground-middleware-express')
.default;
const { makeExecutableSchema } = require('graphql-tools');
const { typeDefs } = require('./schema');
const { resolvers } = require('./resolvers');
// Generate Schema to Use
const schema = makeExecutableSchema({
typeDefs,
resolvers
});
// Init express
const app = express();
app.use(cors(corsOptions));
app.use(cors('*'));
// Set up JWT authentication middleware for Browser Signin
app.use(async (req, res, next) => {
const token = req.headers['authorization'];
// console.log(token);
// Browser token validation
if (token !== 'null') {
try {
// Try to verify the JWT token stored in Browser
const currentUser = await jwt.verify(token, process.env.SECRET);
// console.log(`token: ${typeof token} - env: ${typeof(process.env.SECRET)}`);
req.currentUser = currentUser;
} catch (err) {
console.error(err);
console.log(`${Math.random() * 10} server.js verify error`);
}
}
next();
});
// Set up mongoose connection
var dev_db_url = 'mongodb://localhost:27017/car';
var mongoDB = process.env.MONGODB_URI || dev_db_url;
mongoose.connect(mongoDB, {
useCreateIndex: true,
useFindAndModify: false,
useNewUrlParser: true,
useUnifiedTopology: true
});
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('open', () => console.info('Database connected!✨'));
db.on('error', console.error.bind(console, 'MongoDB connection error:😢'));
// GraphQL use Graphical UI
//app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));
app.get('/playground', expressPlayground({ endpoint: '/graphql' }));
// Connect schema to GraphQL
app.use(
'/graphql',
bodyParser.json(),
graphqlExpress(({ currentUser }) => ({
schema,
context: {
Car,
User,
currentUser
}
}))
);
// Deployment setup
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'))
})
}
const port = process.env.PORT || 4444;
app.listen(port, () => `Server running on port ${port} 🔥`);