-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathserver.js
81 lines (63 loc) · 2.06 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
require('dotenv').config()
const path = require('path')
const express = require('express')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
const morgan = require('morgan')
const cors = require('cors')
const { ApolloServer } = require('apollo-server-express')
const app = express()
const typeDefs = require('./graphql/typeDefs')
const resolvers = require('./graphql/resolvers')
const { Character, Location, Episode } = require('./graphql/sources')
const handle = require('./handlers')
const routes = require('./routes')
const LOCAL = `mongodb://localhost:27017/rickmorty${process.env.NODE_ENV === 'test' ? '-test' : ''}`
const db = process.env.NODE_ENV === 'production' ? process.env.DATABASE : LOCAL
const server = new ApolloServer({
typeDefs,
resolvers,
introspection: true,
playground: true,
validationRules: [handle.depth(4)],
dataSources: () => ({
character: new Character(),
location: new Location(),
episode: new Episode(),
}),
})
mongoose.connect(db, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true })
mongoose.Promise = global.Promise
mongoose.connection.on('error', (err) => {
console.error(`→ ${err.message}`)
})
/* istanbul ignore next */
if (app.get('env') !== 'test') {
app.use(
morgan(':status | :method :url :response-time ms | :remote-addr', {
skip: (req) => req.method !== 'GET',
}),
)
}
app.use(cors())
app.set('trust proxy', 1)
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use('/api/character/avatar', express.static(path.join(__dirname, 'images')))
app.use('/api', routes)
server.applyMiddleware({ app })
app.use(handle.error.notFound)
app.use(handle.error.productionErrors)
const PORT = process.env.PORT || 8080
app.listen(PORT, () =>
console.log(
'\x1b[34m%s\x1b[0m',
`
${app.get('env').toUpperCase()}
REST → http://localhost:${PORT}/api/
GraphQL → http://localhost:${PORT}${server.graphqlPath}/
Database → ${mongoose.connection.host}/${mongoose.connection.name}
`,
),
)
module.exports = app