Make server renderer a class
This commit is contained in:
parent
f93a980854
commit
fd2b8823e6
|
@ -2,7 +2,7 @@ import express from 'express'
|
|||
import helmet from 'helmet'
|
||||
import expressStaticGzip from 'express-static-gzip'
|
||||
import config from '../config/config.json'
|
||||
import { serverRender } from './utils/serverRender'
|
||||
import { ServerRenderer } from './utils/serverRender'
|
||||
import { Scanner } from './utils/scanner'
|
||||
|
||||
const port = process.env.PORT || 3000
|
||||
|
@ -29,7 +29,8 @@ app.get('/favicon.ico', (req, res) => {
|
|||
res.status(404).send('Not Found !!!')
|
||||
})
|
||||
|
||||
app.get('*', serverRender)
|
||||
const serverRenderer = new ServerRenderer()
|
||||
app.get('*', serverRenderer.render)
|
||||
|
||||
app.listen(port, function (error) {
|
||||
if (error) {
|
||||
|
|
|
@ -8,22 +8,29 @@ import manifest from '../../public/static/manifest.json'
|
|||
import config from '../../config/config.json'
|
||||
import head from '../../config/head.json'
|
||||
|
||||
export function serverRender (req, res, next) {
|
||||
const activeRoute = routes.find((route) => matchPath(req.url, route)) || {}
|
||||
export class ServerRenderer {
|
||||
|
||||
const promise = activeRoute.getData
|
||||
? activeRoute.getData(req.path)
|
||||
: Promise.resolve()
|
||||
constructor () {
|
||||
console.log('ServerRenderer')
|
||||
}
|
||||
|
||||
promise.then((data) => {
|
||||
const markup = renderToString(
|
||||
<Router location={req.url} context={{ data }}>
|
||||
<App/>
|
||||
</Router>
|
||||
)
|
||||
|
||||
res.status(200).send(renderFullPage(markup, data))
|
||||
}).catch(next)
|
||||
render (req, res, next) {
|
||||
const activeRoute = routes.find((route) => matchPath(req.url, route)) || {}
|
||||
|
||||
const promise = activeRoute.getData
|
||||
? activeRoute.getData(req.path)
|
||||
: Promise.resolve()
|
||||
|
||||
promise.then((data) => {
|
||||
const markup = renderToString(
|
||||
<Router location={req.url} context={{ data }}>
|
||||
<App/>
|
||||
</Router>
|
||||
)
|
||||
|
||||
res.status(200).send(renderFullPage(markup, data))
|
||||
}).catch(next)
|
||||
}
|
||||
}
|
||||
|
||||
function renderFullPage (html, data) {
|
||||
|
|
Loading…
Reference in New Issue