-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.js
72 lines (58 loc) · 2.24 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import 'babel-polyfill'
import express from 'express'
import cookieParser from 'cookie-parser'
import webpack from 'webpack'
import webpackDevMiddleware from 'webpack-dev-middleware'
import webpackHotMiddleware from 'webpack-hot-middleware'
import webpackHotServerMiddleware from 'webpack-hot-server-middleware'
import clientConfig from '../webpack/client.dev'
import serverConfig from '../webpack/server.dev'
import { findVideos, findVideo } from './api'
const DEV = process.env.NODE_ENV === 'development'
const publicPath = clientConfig.output.publicPath
const outputPath = clientConfig.output.path
const app = express()
// JWTOKEN COOKIE - in a real app obviously you set this after signup/login:
app.use(cookieParser())
app.use((req, res, next) => {
const cookie = req.cookies.jwToken
const jwToken = 'fake' // TRY: set to 'real' to authenticate ADMIN route
if (cookie !== jwToken) {
res.cookie('jwToken', jwToken, { maxAge: 900000 })
req.cookies.jwToken = jwToken
}
next()
})
// API
app.get('/api/videos/:category', async (req, res) => {
const jwToken = req.headers.authorization.split(' ')[1]
const data = await findVideos(req.params.category, jwToken)
res.json(data)
})
app.get('/api/video/:slug', async (req, res) => {
const jwToken = req.headers.authorization.split(' ')[1]
const data = await findVideo(req.params.slug, jwToken)
res.json(data)
})
// UNIVERSAL HMR + STATS HANDLING GOODNESS:
if (DEV) {
const multiCompiler = webpack([clientConfig, serverConfig])
const clientCompiler = multiCompiler.compilers[0]
app.use(webpackDevMiddleware(multiCompiler, { publicPath, stats: { colors: true } }))
app.use(webpackHotMiddleware(clientCompiler))
app.use(
// keeps serverRender updated with arg: { clientStats, outputPath }
webpackHotServerMiddleware(multiCompiler, {
serverRendererOptions: { outputPath }
})
)
}
else {
const clientStats = require('../buildClient/stats.json') // eslint-disable-line import/no-unresolved
const serverRender = require('../buildServer/main.js').default // eslint-disable-line import/no-unresolved
app.use(publicPath, express.static(outputPath))
app.use(serverRender({ clientStats, outputPath }))
}
app.listen(3000, () => {
console.log('Listening @ http://localhost:3000/')
})