Created
January 25, 2020 19:23
-
-
Save prof3ssorSt3v3/24c3ececa00c8397ad18fe8284f5cf2a to your computer and use it in GitHub Desktop.
Using Express JS to serve static files
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
"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