Skip to content

Instantly share code, notes, and snippets.

@tadeaspetak
Last active February 27, 2022 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tadeaspetak/be370d48d9e995adfdc099b5bc542587 to your computer and use it in GitHub Desktop.
Save tadeaspetak/be370d48d9e995adfdc099b5bc542587 to your computer and use it in GitHub Desktop.
Front & Fullstack Security
import express from "express";
import fs from "fs";
import helmet from "helmet";
import https from "https";
import path from "path";
const app = express();
// avoid having to manually tweak CSP, HSTS, X-Powered-By, MIME-sniffing, etc.; set the strictest CSP possible
app.use(helmet({ contentSecurityPolicy: { directives: { defaultSrc: "'self'" } } }));
app.use(express.json());
const client = path.resolve(__dirname, "../build");
if (fs.existsSync(client)) app.use(express.static(client));
app.get("/healthz", (_, res) => { res.send({ message: "We're live 🚀" }); });
const httpsOptions = {
key: fs.readFileSync(path.join(__dirname, "./tls/cert.key")),
cert: fs.readFileSync(path.join(__dirname, "./tls/cert.pem")),
};
const portHttps = process.env.PORT_HTTPS || 8080;
https.createServer(httpsOptions, app).listen(portHttps, async () => {
console.log(`HTTPS server listening at ${portHttps}`); // eslint-disable-line no-console
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment