Created
May 5, 2015 15:04
-
-
Save Ollo/7b50fb24b500d553b281 to your computer and use it in GitHub Desktop.
simple node http server
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
"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