-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
109 lines (100 loc) · 3.06 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
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
import express from 'express'
import bodyParser from 'body-parser'
import cors from 'cors'
import sniffer from 'request-ip'
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express'
import jwt from 'jsonwebtoken'
import schema from './graphql/schema'
import db from './connectors'
import getConfig from './env/config'
const config = getConfig(process.env.NODE_ENV)
const app = express()
// ENVIRONMENT CONFIG
if (process.env.NODE_ENV !== 'production') {
process.on('unhandledRejection', (reason, promise) => {
console.log(
'Unhandled Rejection at:', promise,
'Reason:', reason,
)
})
process.on('uncaughtException', (error) => {
console.error(error)
process.exit(1)
})
}
// MIDDLEWARE
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.use(cors(config.CORS_OPTIONS))
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
// AUTHENTICATION
app.use(async (req) => {
try {
const token = req.headers.authorization
const { person } = await jwt.verify(token, config.SECRET)
req.person = person
return req.next()
} catch (e) {
return req.next()
}
})
// GRAPHQL
app.use('/graphql', bodyParser.json(), graphqlExpress((req) => {
console.log('NEW GRAPHQL REQUEST:', req.body)
const context = {
settings: {
ENV: process.env.NODE_ENV,
PORT: config.PORT,
SECRET: config.SECRET,
},
person: req.person,
db,
}
return {
schema,
context,
rootValue: null,
formatError: error => ({
message: error.message,
locations: error.locations,
stack: error.stack,
path: error.path,
}),
debug: true,
}
}))
app.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
}))
// EVENT LOGGING
app.all('*', async (req, res, next) => {
// FUTURE: Add request counters eg: req.counter += 1
console.log(`
-- Person hitting route --
HTTP METHOD: ${req.method}
ORIGINAL URL: ${req.originalUrl}
ROUTE: ${req.route.path}
URL: ${req.url}
USER AGENT: ${req.headers['user-agent']}
USER IP: ${sniffer.getClientIp(req)}
PERSON: ${req.person}
TIMESTAMP: ${Date.now()}
`)
return next()
})
// SPLAT ROUTE
app.get('*', async (req, res, next) => res.status(404).render('error/404'))
// ERRORS
app.use(async (err, req, res, next) => {
res.status(500).render('error/500')
throw err
})
// START
app.listen(config.PORT, () => {
console.log('┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓')
console.log(`┃ GRAPHQL ENDPOINT: http://localhost:${config.PORT}/graphql ┃`)
console.log(`┃ GRAPHIQL ENDPOINT: http://localhost:${config.PORT}/graphiql ┃`)
console.log('┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛')
})
export default db