Skip to content

Instantly share code, notes, and snippets.

@j7nn7k
Created February 21, 2018 15: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 j7nn7k/b39d0f040f59c96b2f8b723f7c9fb8a7 to your computer and use it in GitHub Desktop.
Save j7nn7k/b39d0f040f59c96b2f8b723f7c9fb8a7 to your computer and use it in GitHub Desktop.
//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