-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
68 lines (52 loc) · 1.76 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
const express = require('express');
const cors = require('cors');
const path = require('path');
const mongoose = require('mongoose');
const config = require('config');
const cookieParser = require("cookie-parser");
const { verify } = require("jsonwebtoken");
// const { validateTokensMiddleware } = require('./modules/authHelpers/authMiddleware')
// #6 Initialize an Express application
const app = express();
// Allow cross-origin
app.use(cors());
// app.use(validateTokensMiddleware);
// DB Config
const db = config.get('mongoURI');
const ACCESS_TOKEN_SECRET = config.get('ACCESS_TOKEN_SECRET');
// Connect to Mongo
mongoose
.connect(db, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false
}) // Adding new mongo url parser
.then(() => console.log('MongoDB Connected...'))
.catch(err => console.log(err));
const { ApolloServer } = require('apollo-server-express');
const typeDefs = require('./modules/graphqlschemas/index');
const resolvers = require('./modules/resolvers/index');
// #5 Initialize an Apollo server
const server = new ApolloServer({
typeDefs: typeDefs,
resolvers: resolvers,
context: ({ req, res }) => ({ req, res })
});
app.use(cookieParser());
app.use((req, _, next) => {
const accessToken = req.cookies["access-token"];
try {
const data = verify(accessToken, ACCESS_TOKEN_SECRET);
(req).userId = data.userId;
} catch {}
next();
});
// #7 Use the Express application as middleware in Apollo server
server.applyMiddleware({ app });
app.use(express.static('public'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'public', 'index.html'))
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));