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

View File

@ -8,7 +8,13 @@ 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 {
constructor () {
console.log('ServerRenderer')
}
render (req, res, next) {
const activeRoute = routes.find((route) => matchPath(req.url, route)) || {} const activeRoute = routes.find((route) => matchPath(req.url, route)) || {}
const promise = activeRoute.getData const promise = activeRoute.getData
@ -24,6 +30,7 @@ export function serverRender (req, res, next) {
res.status(200).send(renderFullPage(markup, data)) res.status(200).send(renderFullPage(markup, data))
}).catch(next) }).catch(next)
}
} }
function renderFullPage (html, data) { function renderFullPage (html, data) {