-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (107 loc) · 3.1 KB
/
index.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**--external-- */
const { ApolloServer } = require('apollo-server-express');
const { ApolloServerPluginDrainHttpServer } = require('apollo-server-core');
const dotEnv = require('dotenv');
const express = require('express');
const http = require('http');
const mongoose = require('mongoose');
const cors = require('cors');
const cookieParser = require('cookie-parser');
/**--relative-- */
const { resolvers } = require('./resolvers');
const { typeDefs } = require('./schema');
const initMailTransporter = require('./mailTransporters');
const buildDataLoaders = require('./dataloaders');
const { userRoutes, pingRoutes, cspRoutes } = require('./routes');
const { verifyToken, globalErrorHandler, setCsp } = require('./middleware');
const app = express();
dotEnv.config();
initMailTransporter();
/**
* This will set Content-Security-Policy for each request
*/
app.use(setCsp);
/**
* This will expose cookies on req.cookies
*/
app.use(cookieParser());
/**
* This will allow request from only specific origins
*/
const whitelist = [
'http://localhost:3000',
'https://resource-library.netlify.app',
'https://studio.apollographql.com',
];
const corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback({ message: 'Not allowed by CORS', statusCode: 500 });
}
},
credentials: true,
};
app.use(cors(corsOptions));
/**
* This will add JSON payload in as req.body Object
*/
app.use(
express.json({
type: [
'application/json',
'application/csp-report',
'application/reports+json',
],
})
);
/**
* This will populate URL encoded payload as req.body Object
*/
app.use(express.urlencoded({ extended: true }));
app.use(userRoutes);
app.use(pingRoutes);
app.use(cspRoutes);
app.use(verifyToken);
app.use(globalErrorHandler);
/**
* Instead of using app.listen, we are creating httpServer. httpServer requires a function which
* handles req and res. Here we are using app for that purpose.
* So, when request arrives control will flow in this way:
* httpServer -> app -> middleware ->apollo server
*/
const httpServer = http.createServer(app);
const { PASSWORD, DB } = process.env;
const DB_URL = `mongodb+srv://Jeel2308:${PASSWORD}@cluster0.erkx1.mongodb.net/${DB}?retryWrites=true&w=majority`;
const startServer = async () => {
try {
await mongoose.connect(DB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log('Mongodb connected');
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req }) => {
return {
user: req.user,
loaders: buildDataLoaders(),
};
},
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});
await server.start();
console.log('Apollo server started');
/**
* Internally, it registers route middleware for /graphql route on app.
*/
server.applyMiddleware({ app });
await httpServer.listen({ port: process.env.PORT });
console.log('Express server started');
} catch (e) {
console.log(e);
}
};
startServer();