-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
57 lines (51 loc) · 1.58 KB
/
index.ts
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
import express from 'express'
import dotenv from 'dotenv'
import bodyParser from 'body-parser'
import { Resolvers, TypeDefs } from './src/graphql/schema'
import cors from 'cors'
import './src/db/connection'
import { isLoggedIn } from './src/middleware/auth'
import cookieParser from 'cookie-parser'
import { startApolloServer } from './src/services/apolloServer'
import path from 'path'
import { create } from 'express-handlebars'
import router from './src/routes'
dotenv.config()
const cookieSession = require('cookie-session')
const app = express()
export const hbs = create({
layoutsDir: path.join(__dirname, 'src', 'views', 'layouts'),
defaultLayout: 'main',
extname: 'hbs',
})
const port = process.env.PORT || 4000
if (process.env.NODE_ENV === 'production')
app.use(
cookieSession({
keys: [process.env.SECRET],
maxAge: 24 * 60 * 60 * 1000,
})
)
app.use(cookieParser())
app.use(cors())
app.use(bodyParser.json())
app.use(isLoggedIn)
app.engine('hbs', hbs.engine)
app.set('view engine', 'hbs')
app.set('views', path.join(__dirname, 'src', 'views'))
app.use(express.static(path.resolve(__dirname, 'public')))
app.use('/', router)
;(async () => {
await startApolloServer(app, TypeDefs, Resolvers)
})()
if (process.env.NODE_ENV === 'production') {
// Express will serve Production Assets
app.use(express.static(path.join(__dirname, '../', 'client', 'build')))
//Express will serve up the index.html
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../', 'client', 'build', 'index.html'))
})
}
app.listen(port, () => {
console.log(`Server listening on port ${port}`)
})