Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//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