Skip to content

Instantly share code, notes, and snippets.

@Ollo
Created May 5, 2015 15:04
Show Gist options
  • Save Ollo/7b50fb24b500d553b281 to your computer and use it in GitHub Desktop.
Save Ollo/7b50fb24b500d553b281 to your computer and use it in GitHub Desktop.
simple node http server
"use strict";
var https = require('https');
var fs = require('fs');
var qs = require('querystring');
var options = {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
};
var PORT = 8181;
function handleRequest(req, res){
//Process Post Request
if(req.method === "POST"){
var data = '';
req.on('data', function(chunk){
data += chunk;
});
req.on('end', function(){
var parseData = qs.parse(data);
var prettyData = JSON.stringify(parseData, null, 2);
console.log("Post request with:\n" + prettyData);
res.end(prettyData);
});
} else { //Send a simple response
res.end('Everything works');
}
}
//Create a server
var server = https.createServer(options, handleRequest);
//Start server
server.listen(PORT, function(){
console.log("Server listening on: https://localhost:" + PORT);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment