namesny-com/src/server.js

51 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-12-29 22:07:50 +00:00
import express from 'express'
2019-10-19 20:03:31 +00:00
import helmet from 'helmet'
2019-03-27 18:10:50 +00:00
import expressStaticGzip from 'express-static-gzip'
2019-10-19 20:03:31 +00:00
import config from '../config/config.json'
2019-10-30 19:17:20 +00:00
import path from 'path'
import jsonfile from 'jsonfile'
2019-10-29 21:49:13 +00:00
import { ServerRenderer } from './utils/serverRender'
2019-02-20 20:27:34 +00:00
import { Scanner } from './utils/scanner'
2019-02-18 20:18:55 +00:00
2019-02-16 18:32:32 +00:00
const port = process.env.PORT || 3000
2018-12-29 22:07:50 +00:00
const app = express()
2017-07-15 14:52:09 +00:00
2019-02-20 20:27:34 +00:00
const scanner = new Scanner()
scanner.scan()
2019-02-20 18:13:33 +00:00
2019-10-19 20:03:31 +00:00
app.use(helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'", `*.${config.baseUrl}`],
scriptSrc: ["'self'", "'unsafe-inline'", "'unsafe-eval'", `*.${config.baseUrl}`],
styleSrc: ["'self'", 'fonts.googleapis.com', 'fonts.gstatic.com', 'maxcdn.bootstrapcdn.com'],
fontSrc: ["'self'", 'fonts.googleapis.com', 'fonts.gstatic.com', 'maxcdn.bootstrapcdn.com'],
imgSrc: ['*'],
workerSrc: false,
blockAllMixedContent: true
}
}))
2019-03-27 18:10:50 +00:00
app.use('/static', expressStaticGzip('public/static'))
2019-01-21 18:21:51 +00:00
2019-02-16 16:56:43 +00:00
app.get('/favicon.ico', (req, res) => {
2019-02-16 18:32:32 +00:00
res.status(404).send('Not Found !!!')
2019-02-16 16:56:43 +00:00
})
2019-10-30 19:17:20 +00:00
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))
2017-04-09 19:09:27 +00:00
2019-02-16 18:32:32 +00:00
app.listen(port, function (error) {
2017-07-15 14:52:09 +00:00
if (error) {
2019-02-16 18:32:32 +00:00
console.error(error)
2017-07-15 14:52:09 +00:00
} else {
2019-02-16 18:32:32 +00:00
console.info('[Server] Listening on port %s', port)
2017-04-09 19:09:27 +00:00
}
2017-07-15 14:52:09 +00:00
})