-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
79 lines (66 loc) · 2.21 KB
/
app.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import express from 'express'
import compress from 'compression'
import cors from 'cors'
import mongoose from 'mongoose'
import config from './utils/config'
import userRouter from './controllers/userRoutes'
import authRouter from './controllers/authRoutes'
import productRouter from './controllers/productRoutes'
import orderRouter from './controllers/orderRoutes'
import middleware from './utils/middleware'
import helmet from 'helmet'
//modules for server side rendering. Rating from material-ui/lab breaks ssr. Migrate to v5 before implementing
/*
import React from 'react'
import ReactDOMServer from 'react-dom/server'
import { StaticRouter } from 'react-router-dom'
import App from './client/src/App'
import { ServerStyleSheets, ThemeProvider } from '@material-ui/core'
import theme from './client/src/theme'
*/
const app = express()
mongoose.connect(config.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true })
.then(() => console.log('connected to mongodb'))
.catch(error => {
console.log(`error connecting to mongodb. error: ${error.message}`)
})
const corsOptions = {
exposedHeaders: 'xTotalCount'
}
app.use(cors(corsOptions))
app.use(helmet())
app.use(express.static('build'))
app.use(express.json())
app.use('/api', userRouter)
app.use('/auth', authRouter)
app.use('/api', productRouter)
app.use('/api', orderRouter)
// server side rendering
/*
app.get('*', (req, res) => {
const sheets = new ServerStyleSheets()
const context = {}
const markup = ReactDOMServer.renderToString(
sheets.collect(
<StaticRouter location={req.url} context={context}>
<ThemeProvider theme={theme}>
<MainRouter />
</ThemeProvider>
</StaticRouter>
)
)
if (context.url) {
return res.redirect(303, context.url)
}
const css = sheets.toString()
let indexHTML = fs.readFileSync(path.resolve(__dirname, '../build/index.html'), {
encoding: 'utf-8'
})
indexHTML = indexHTML.replace(`<div id="root"></div>`, `<div id="root">${markup}</div>`)
indexHTML = indexHTML.replace(`<style id="jss-server-side"></style>`, `<style id="jss-server-side">${css}</style>`)
res.send(indexHTML)
})
*/
app.use(middleware.unknownEndpoint)
app.use(middleware.errorHandler)
export default app