Created
March 7, 2015 10:32
-
-
Save HowardvanRooijen/08c61a4af8d873ad0ee1 to your computer and use it in GitHub Desktop.
HTTP Proxy Node Service for forwarding data into Azure Event Hubs HTTPS endpoint from a low power HTTP device
This file contains hidden or 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
var express = require('express'); | |
var app = express(); | |
var server = require('http').createServer(app); | |
var https = require('https'); | |
function HttpPostToAzure(jsonData) | |
{ | |
// Build the HTTP Post | |
// Currently using hardcoded auth string, this auth string could come via the device | |
var post_options = | |
{ | |
host: 'YOUR-NAMESPACE.servicebus.windows.net', | |
port: 443, | |
path: 'YOUR-PATH/messages', | |
method: 'POST', | |
headers: { | |
'User-Agent': 'NodeJS-Proxy', | |
'Content-Type': 'application/json; charset=utf-8', | |
'Authorization': 'SharedAccessSignature sr=ADD-YOU-SAS-HERE', | |
'Content-Length': Buffer.byteLength(jsonData) | |
} | |
}; | |
// Set up the request | |
var post_req = https.request(post_options, function(res) | |
{ | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
console.log('Response: ' + chunk); | |
}); | |
}); | |
// post the data | |
post_req.write(jsonData); | |
post_req.end(); | |
} | |
var port = 4000; | |
server.listen(port, function () | |
{ | |
console.log('Server listening at port %d', port); | |
}); | |
// HTTP Post Listener | |
app.post('/recv', function (req, res) | |
{ | |
var body = ''; | |
req.on('data', function (data) { | |
body += data; | |
}); | |
req.on('end', function () | |
{ | |
console.log(body); | |
HttpPostToAzure(body); | |
}); | |
res.writeHead(200); | |
res.end(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment