-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
40 lines (33 loc) · 1.14 KB
/
app.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
const express = require('express');
const app = express();
const graphqlHttp = require('express-graphql');
const mongoose = require('mongoose');
require('dotenv').config();
const graphQLSchema = require('./graphql/schema/index');
const graphQlResolvers = require('./graphql/resolvers/index');
const checkAuth = require('./middleware/check-auth');
// middleware
express.json();
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST,GET,OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
if (req.method === 'OPTIONS') {
return res.sendStatus(200);
}
next();
});
app.use(checkAuth);
app.use('/graphql', graphqlHttp({
schema: graphQLSchema,
rootValue: graphQlResolvers,
graphiql: true
}));
// connect to MongoDB
mongoose.connect(`mongodb+srv://${process.env.ATLAS_USER}:${process.env.ATLAS_PW}@cluster0-vxymi.mongodb.net/${process.env.DB_NAME}?retryWrites=true`, {useNewUrlParser: true})
.then(() => {
app.listen(process.env.PORT);
})
.catch(err => {
console.log(err);
});