Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created January 25, 2020 19:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save prof3ssorSt3v3/24c3ececa00c8397ad18fe8284f5cf2a to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/24c3ececa00c8397ad18fe8284f5cf2a to your computer and use it in GitHub Desktop.
Using Express JS to serve static files
"use strict";
const express = require("express");
const app = express();
const port = process.env.port || 5005;
// https://expressjs.com/en/4x/api.html#express.static
let options = {
dotfiles: "ignore", //allow, deny, ignore
etag: true,
extensions: ["htm", "html"],
index: false, //to disable directory indexing
maxAge: "7d",
redirect: false,
setHeaders: function(res, path, stat) {
//add this header to all static responses
res.set("x-timestamp", Date.now());
}
};
app.use(express.static("public", options));
//you can use https://favicon.io/favicon-generator/ to create the favicon.ico
app.get("/", (req, res) => {
let img = `<img src="/img/cotton-candy.gif"/>`;
let secret = `<a href="/.htaccess">secret</a>`;
let html = `<!Doctype html><html><head><title>Sample</title></head>`;
html += `<body><h1>Sample HTML</h1><main>${secret}</main></body></html>`;
res.send(html);
});
app.listen(port, err => {
if (err) {
return console.log("ERROR", err);
}
console.log(`Listening on port ${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment