Skip to content

Instantly share code, notes, and snippets.

@caub
Last active Oct 15, 2018
Embed
What would you like to do?
https with letsencrypt
// d run -d --name=ok -p 443:6100 -p 80:6000 --workdir=/home/node -v /etc/letsencrypt/:/etc/letsencrypt/:ro -v $PWD/server.js:/home/node/server.js node node server
var https = require("https");
var http = require("http");
var fs = require("fs");
var p="/etc/letsencrypt/live/ulti.pw/";
var hs=https.createServer({
key: fs.readFileSync(p+"privkey.pem")+"",
cert:fs.readFileSync(p+"cert.pem")+"",
ca: fs.readFileSync(p+"chain.pem")+""
}, (req,res) => {
res.write("ok");
res.end();
});
var h=http.createServer((req,res) => {
if (!req.connection.encrypted && (req.method === "GET" || req.method === "HEAD")) {
res.writeHead(308, {location: "https://" + req.headers.host + req.url});
return res.end();
}
res.write("ko");
res.end();
});
hs.listen(process.env.HTTPS_PORT || 6100);
h.listen(process.env.HTTP_PORT || 6000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment