Skip to content

Instantly share code, notes, and snippets.

@chrisjmendez
Last active April 8, 2016 03:25
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 chrisjmendez/7e0737e06448c8dbdfe86d4cde63f575 to your computer and use it in GitHub Desktop.
Save chrisjmendez/7e0737e06448c8dbdfe86d4cde63f575 to your computer and use it in GitHub Desktop.
Simple NodeJS Server to use in conjunction with Let's Encypt
const http = require('http'),
fs = require('fs'),
path = require('path'),
contentTypes = require('./utils/content-types'),
sysInfo = require('./utils/sys-info'),
env = process.env;
let server = http.createServer(function (req, res) {
let url = req.url;
if (url == '/') {
url += 'index.html';
}
// IMPORTANT: Your application HAS to respond to GET /health with status 200
// for OpenShift health monitoring
if (url == '/health') {
res.writeHead(200);
res.end();
} else if (url.indexOf('/info/') == 0) {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Cache-Control', 'no-cache, no-store');
res.end(JSON.stringify(sysInfo[url.slice(6)]()));
} else {
/*
fs.readFile('./static' + url, function (err, data) {
if (err) {
res.writeHead(404);
res.end();
} else {
let ext = path.extname(url).slice(1);
res.setHeader('Content-Type', contentTypes[ext]);
if (ext === 'html') {
res.setHeader('Cache-Control', 'no-cache, no-store');
}
res.end(data);
}
});
*/
if (!req.url.match(/\.well-known\/acme-challenge/)){
res.writeHead(403);
res.end('Illegal address');
} else {
var filepath = '../data/' + req.url;
fs.readFile(filepath, function (err, data){
if(err){
res.writeHead(404);
res.end('File not found');
}else{
res.writeHead(200, {'Content-Type' : 'text'});
res.end(data, 'utf-8');
}
});
}
}
});
server.listen(env.NODE_PORT || 3000, env.NODE_IP || 'localhost', function () {
console.log(`Application worker ${process.pid} started...`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment