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

View File

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