-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
47 lines (36 loc) · 1.23 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const express = require('express')
const app = express()
const port = process.env.PORT || 5000
const path = require('path')
const fs = require('fs')
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const replaceSeoData = function (response, opts = {}) {
const filePath = path.resolve(__dirname, './build', 'index.html')
fs.readFile(filePath, 'utf8', function (err, indexHTMLData) {
if (err) {
return console.log(err)
}
indexHTMLData = indexHTMLData.replace(/__TITLE__/g, 'Example')
response.send(indexHTMLData)
// optionally you can fetch meta from api and replace it for each unique url
// fetchSeoData(indexHTMLData, opts, function(data) {
// response.send(data)
// })
})
}
app.get('/', function (request, response) {
replaceSeoData(response, { hostname: request.hostname })
})
app.use(express.static(path.resolve(__dirname, './build')))
app.get('*', function (request, response) {
const filePath = path.resolve(__dirname, './build', 'index.html')
fs.readFile(filePath, 'utf8', function (err, indexHTMLData) {
if (err) {
return console.log(err)
}
response.send(indexHTMLData)
})
})
app.listen(port, () => console.log(`Listening on port ${port}`))