Created
February 21, 2018 15:30
-
-
Save j7nn7k/b39d0f040f59c96b2f8b723f7c9fb8a7 to your computer and use it in GitHub Desktop.
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
//index.js | |
var iothub = require('azure-iothub'); | |
var config = require("./config"); | |
var registry = iothub.Registry.fromConnectionString(config.connectionString); | |
module.exports = function (context, req) { | |
// get devices registered in your iot hub | |
const query = registry.createQuery("SELECT * FROM devices", 100); | |
query.nextAsTwin(function (error, result) { | |
if (!error) { | |
context.log(JSON.stringify(result)); | |
context.res = { | |
body: JSON.stringify(result), | |
headers: { | |
'Content-Type': 'application/json' | |
} | |
}; | |
} else { | |
context.res = { | |
status: 400, | |
body: error.message + ' : ' + error.responseBody | |
}; | |
} | |
context.done(); | |
}); | |
}; | |
// config.js | |
var config = {}; | |
config.connectionString = 'IOT_HUB_CONNECTION_STRING'; | |
module.exports = config; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment