Skip to content

Instantly share code, notes, and snippets.

@aws-joe
Created November 10, 2016 08:30
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 aws-joe/87d3f30a94b8eb28fa035bd8a712f566 to your computer and use it in GitHub Desktop.
Save aws-joe/87d3f30a94b8eb28fa035bd8a712f566 to your computer and use it in GitHub Desktop.
//to run:
// # node blue-button-http.js &
//make sure that the NPM modules below are installed
var http = require('http');
var qs = require('querystring');
var bb = require('blue-button');
//Lets define a port we want to listen to
const PORT=8000;
var conn_count = 0;
//We need a function which handles requests and send response
function handleRequest(request, response){
conn_count++;
if (request.method == 'POST') {
var body = [];
request.on('data', function(chunk) {
body.push(chunk);
}).on('end', function() {
body = Buffer.concat(body).toString();
});
console.log("Processing Connection: "+conn_count);
request.on('end', function () {
var json=bb.parse(body);
response.end(JSON.stringify(json, null, 4));
});
} else {
response.end('Issue processing: ' + request.url);
}
}
//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT, function(){
//Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment