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