Skip to content

Instantly share code, notes, and snippets.

@ivanrad
Last active July 10, 2022 23:39
Show Gist options
  • Save ivanrad/84540e8d80f19aa5f01c to your computer and use it in GitHub Desktop.
Save ivanrad/84540e8d80f19aa5f01c to your computer and use it in GitHub Desktop.
quick and dirty node.js maintenance (503 all -- well, almost all) server
var http = require('http');
var fs = require('fs');
var health_ok_body = "OK\n";
var default_503_body = "Back soon!\n";
var maintenance_file = __dirname + "/maintenance.html";
http.createServer(function (request, response) {
console.log('request: ' + request.url);
if (request.url == "/_health") {
response.writeHead(200, { "Content-Type": "text/plain",
"Content-Length": health_ok_body.length });
response.end(health_ok_body, "utf-8");
} else {
fs.readFile(maintenance_file, function(err, body) {
var content_type = "text/html";
if (err) {
content_type = "text/plain";
body = default_503_body;
}
response.writeHead(503, { "Content-Type": content_type,
"Content-Length": body.length,
"Retry-After": 60 });
response.end(body, "utf-8");
});
}
}).listen(3000, '127.0.0.1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment