Read head.json instead of importing

This commit is contained in:
LordMathis 2019-10-30 20:17:20 +01:00
parent cc9ba52c6b
commit 4ce788e750
No known key found for this signature in database
GPG Key ID: 575849FD91CE470C
2 changed files with 18 additions and 8 deletions

View File

@ -2,6 +2,8 @@ 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 path from 'path'
import jsonfile from 'jsonfile'
import { ServerRenderer } from './utils/serverRender' import { ServerRenderer } from './utils/serverRender'
import { Scanner } from './utils/scanner' import { Scanner } from './utils/scanner'
@ -29,8 +31,15 @@ app.get('/favicon.ico', (req, res) => {
res.status(404).send('Not Found !!!') res.status(404).send('Not Found !!!')
}) })
const serverRenderer = new ServerRenderer() let head = jsonfile.readFileSync(path.join(process.cwd(), 'config/head.json'))
app.get('*', serverRenderer.render) if (head == null) {
head = {
"scripts": []
}
}
const serverRenderer = new ServerRenderer(head)
app.get('*', serverRenderer.render.bind(serverRenderer))
app.listen(port, function (error) { app.listen(port, function (error) {
if (error) { if (error) {

View File

@ -6,17 +6,18 @@ import routes from './routes'
import serialize from 'serialize-javascript' import serialize from 'serialize-javascript'
import manifest from '../../public/static/manifest.json' 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'
export class ServerRenderer { export class ServerRenderer {
constructor () { constructor (head) {
console.log('ServerRenderer') this.head = head
} }
render (req, res, next) { render (req, res, next) {
const activeRoute = routes.find((route) => matchPath(req.url, route)) || {} const activeRoute = routes.find((route) => matchPath(req.url, route)) || {}
const head = this.head
const promise = activeRoute.getData const promise = activeRoute.getData
? activeRoute.getData(req.path) ? activeRoute.getData(req.path)
: Promise.resolve() : Promise.resolve()
@ -28,12 +29,12 @@ export class ServerRenderer {
</Router> </Router>
) )
res.status(200).send(renderFullPage(markup, data)) res.status(200).send(renderFullPage(markup, head, data))
}).catch(next) }).catch(next)
} }
} }
function renderFullPage (html, data) { function renderFullPage (html, head, data) {
return ` return `
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>