-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
35 lines (27 loc) · 925 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
const express = require('express')
const graphqlHTTP = require('express-graphql')
const schema = require('./schema')
const mongoose = require('mongoose')
const cors = require('cors')
const path = require('path')
require('dotenv').config()
const app = express()
const PORT = process.env.PORT || 3005
mongoose.connect(
process.env.DB_URI,
{ useNewUrlParser: true },
{ useUnifiedTopology: true }
//{ useMongoClient: true }
)
app.use(cors())
app.use('/graphql', graphqlHTTP({ schema, graphiql: true }))
const dbConnection = mongoose.connection
dbConnection.on('error', err => console.log(`Connection error: ${err}`))
dbConnection.once('open', () => console.log('Connected to DB!'))
app.use(express.static(path.join(__dirname, 'dist')))
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'))
})
app.listen(PORT, err =>
console.log(err ? err : `Server started on port ${PORT}`)
)