Skip to content

Instantly share code, notes, and snippets.

@Athelian
Last active September 21, 2021 13:41
Show Gist options
  • Save Athelian/0f8576d379be64f9e95c984c96049b83 to your computer and use it in GitHub Desktop.
Save Athelian/0f8576d379be64f9e95c984c96049b83 to your computer and use it in GitHub Desktop.
frontend server target build index
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