-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
45 lines (38 loc) · 1.01 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
'use strict'
require('dotenv').config()
const express = require('express')
const bodyParser = require('body-parser-graphql')
const { graphqlExpress } = require('apollo-server-express')
const schema = require('./src/schema')
const jwt = require('express-jwt')
const cors = require('cors')
const loadServices = require('./src/services')
const API_PORT = process.env.API_PORT ? process.env.API_PORT : 3000
loadServices().then((ret) => {
console.log("Ret: ", ret)
// create our express app
const app = express()
app.use(cors())
// auth middleware
const auth = jwt({
secret: process.env.JWT_SECRET,
credentialsRequired: false
});
// graphql endpoint
app.use(
'/graphql',
bodyParser.graphql(),
auth,
graphqlExpress(req => ({
schema,
context: {
user: req.user
}
})
));
app.listen(API_PORT, () => {
console.log(`The server is running on http://localhost:${API_PORT}/graphql`)
});
}).catch(error => {
console.log("Failed to load services: ", error);
});