-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Demo Admin: Adapt server based on Starter (#3098)
- Loading branch information
1 parent
753cd6f
commit ad93dfb
Showing
5 changed files
with
310 additions
and
125 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* eslint-disable no-undef */ | ||
const express = require("express"); | ||
const compression = require("compression"); | ||
const helmet = require("helmet"); | ||
const fs = require("fs"); | ||
|
||
const app = express(); | ||
const port = process.env.APP_PORT ?? 3000; | ||
|
||
let indexFile = fs.readFileSync("./build/index.html", "utf8"); | ||
|
||
// Replace environment variables | ||
indexFile = indexFile.replace(/\$([A-Z_]+)/g, (match, p1) => { | ||
return process.env[p1] || ""; | ||
}); | ||
|
||
app.use(compression()); | ||
app.use( | ||
helmet({ | ||
contentSecurityPolicy: { | ||
directives: { | ||
"script-src": ["'self'", "'unsafe-inline'"], | ||
"img-src": ["'self'", "https:", "data:"], | ||
"default-src": ["'self'", "https:"], | ||
"media-src": ["'self'", "https:"], | ||
"style-src": ["'self'", "https:", "'unsafe-inline'"], | ||
"font-src": ["'self'", "https:", "data:"], | ||
}, | ||
}, | ||
xXssProtection: false, | ||
strictTransportSecurity: { | ||
maxAge: 63072000, | ||
includeSubDomains: true, | ||
preload: true, | ||
}, | ||
}), | ||
); | ||
|
||
app.get("/status/health", (req, res) => { | ||
res.send("OK!"); | ||
}); | ||
|
||
app.use( | ||
express.static("./build", { | ||
index: false, // Don't send index.html for requests to "/" as it will be handled by the fallback route (with replaced environment variables) | ||
setHeaders: (res, path, stat) => { | ||
if (path.endsWith(".js")) { | ||
// The js file is static and the index.html uses a parameter as cache buster | ||
// implemented as suggested by https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#caching_static_assets | ||
res.setHeader("cache-control", "public, max-age=31536000, immutable"); | ||
} else { | ||
// Icons and Fonts could be changed over time, cache for 7d | ||
res.setHeader("cache-control", "public, max-age=604800, immutable"); | ||
} | ||
}, | ||
}), | ||
); | ||
|
||
// As a fallback, route everything to index.html | ||
app.get("*", (req, res) => { | ||
// Don't cache the index.html at all to make sure applications updates are applied | ||
// implemented as suggested by https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#preventing_storing | ||
res.setHeader("cache-control", "no-store"); | ||
res.send(indexFile); | ||
}); | ||
|
||
app.listen(port, () => { | ||
console.log(`Admin app listening at http://localhost:${port}`); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
{ | ||
"name": "comet-demo-admin-server", | ||
"version": "1.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"compression": "^1.7.4", | ||
"express": "^4.18.2" | ||
"compression": "^1.7.5", | ||
"express": "^4.21.1", | ||
"helmet": "^7.2.0" | ||
}, | ||
"scripts": { | ||
"preserve": "envsubst < \"../build/index.html\" > \"/tmp/index.html\" && mv /tmp/index.html ../build/index.html", | ||
"serve": "node server.js" | ||
"engines": { | ||
"node": "22" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.