forked from gadingnst/kampus-scraper
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
38 lines (30 loc) · 987 Bytes
/
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
const http = require('http')
const express = require('express')
const { ApolloServer, ApolloError } = require('apollo-server-express')
const schema = require('./src/schema')
const port = process.env.PORT || 3300
const app = express()
const server = http.createServer(app)
const Apollo = new ApolloServer({
introspection: true,
playground: true,
schema,
})
const serverOnError = err => {
console.log(err)
if (err.code.toLowerCase().includes('time'))
throw new ApolloError('Connection Timeout. Please Try again.', 408)
else
throw new ApolloError('Internal Server Error. Please comeback later.', 500)
}
Apollo.applyMiddleware({ app, path: '/graphql' })
app.get('*', (req, res) => {
res.sendFile(`${__dirname}/public/index.html`)
})
server.listen(port, () => {
console.log(`> GraphQL ready on http://localhost:${port}/graphql`)
})
server.on('error', serverOnError)
module.exports = (req, res) => {
req.on('error', serverOnError)
}