Last active
September 21, 2021 13:41
-
-
Save Athelian/0f8576d379be64f9e95c984c96049b83 to your computer and use it in GitHub Desktop.
frontend server target build index
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const fs = require("fs"); | |
const path = require("path"); | |
const app = express(); | |
const port = process.env.PORT || 8080; | |
const filePath = path.resolve(__dirname, "./build", "index.html"); | |
// Home page route | |
app.get("/", function (request, response) { | |
// Read in index.html | |
fs.readFile(filePath, "utf8", function (err, data) { | |
if (err) return console.log(err); | |
// Find and replace strings | |
const result = data.replace(/\$OG-TITLE/g, "Home"); | |
response.send(result); | |
}); | |
}); | |
// As above | |
app.get("/my-fun-route", function (request, response) { | |
fs.readFile(filePath, "utf8", function (err, data) { | |
if (err) return console.log(err); | |
const result = data.replace(/\$OG-TITLE/g, "My fun route"); | |
response.send(result); | |
}); | |
}); | |
app.use(express.static(path.join(__dirname, "build"))); | |
app.get("*", (req, res) => | |
res.sendFile(path.join(__dirname, "build/index.html")) | |
); | |
app.listen(port, () => console.log(`Listening on port ${port}`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment