Last active
April 8, 2016 03:25
-
-
Save chrisjmendez/7e0737e06448c8dbdfe86d4cde63f575 to your computer and use it in GitHub Desktop.
Simple NodeJS Server to use in conjunction with Let's Encypt
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
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