-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIndex.js
48 lines (40 loc) · 1020 Bytes
/
Index.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
import express from 'express'
import expressGraphQL from 'express-graphql'
import mongoose from 'mongoose'
import bodyParser from 'body-parser'
import cors from 'cors'
import path from 'path'
import schema from './graphql/'
const app = express()
const PORT = process.env.PORT || '4000'
const db =
'mongodb://ecorea:[email protected]:55073/graphql-mongodb-server'
// Connect to MongoDB with Mongoose.
mongoose
.connect(
db,
{
useCreateIndex: true,
useNewUrlParser: true,
},
)
.then(() => console.log('MongoDB connected'))
.catch(err => console.log(err))
app.use(
'/graphql',
cors(),
bodyParser.json(),
expressGraphQL({
schema,
graphiql: true,
}),
)
// Serve static assets if in production.
if (process.env.NODE_ENV === 'production') {
// Set static folder.
app.use(express.static('client/build'))
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
})
}
app.listen(PORT, () => console.log(`Server running on port ${PORT}`))