Make server renderer a class

This commit is contained in:
LordMathis 2019-10-29 22:49:13 +01:00
parent f93a980854
commit fd2b8823e6
No known key found for this signature in database
GPG Key ID: 575849FD91CE470C
2 changed files with 24 additions and 16 deletions

View File

@ -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) {

View File

@ -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) {