-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
78 lines (67 loc) · 1.8 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
require('dotenv').config()
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'production'
}
if (process.env.PORT) {
process.env.SERVER_PORT = process.env.PORT
}
const mongoose = require('mongoose')
const express = require('express')
const cors = require('cors')
const helmet = require('helmet')
const morgan = require('morgan')
const bearerToken = require('express-bearer-token')
const { ApolloServer } = require('apollo-server-express')
const apiRoutes = require('./routes')
const { typeDefs, resolvers } = require('./graphql')
const auth = require('./middleware/auth')
const loader = require('./middleware/loader')
mongoose.set('useCreateIndex', true)
mongoose.set('useUnifiedTopology', true)
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req }) => req,
debug: process.env.NODE_ENV === 'development',
introspection: true,
playground: {
settings: {
'request.credentials': 'include',
'schema.polling.interval': 5000
}
}
})
const app = express()
app.use(morgan('dev', { stream: {
write: msg => console.log(msg)
} }))
app.use(helmet({ contentSecurityPolicy: false }))
app.use(cors({
methods: ['GET', 'POST'],
allowedHeaders: ['Content-Type', 'Authorization']
}))
app.use(bearerToken())
app.use(auth)
app.use(loader)
app.use('/api', apiRoutes)
server.applyMiddleware({ app })
mongoose
.connect(
`mongodb+srv://${process.env.MONGODB_USER}:${process.env.MONGODB_PASS}@${
process.env.MONGODB_CLUSTER
}`,
{ useNewUrlParser: true }
)
.then(() => {
app.listen({ port: process.env.SERVER_PORT }, () =>
console.log(
`Server ready at ${process.env.SERVER_ADDRESS}:${process.env.SERVER_PORT}${
server.graphqlPath
}`,
`NODE_ENV=${process.env.NODE_ENV}`
)
)
})
.catch(err => {
console.log(err)
})