適用於 Node.js 的 Azure IoT 中樞模組Azure IoT Hub modules for Node.js

Azure IoT 中樞是一項完全受控的服務,可在數百萬個 IoT 裝置和一個解決方案後端之間啟用可靠且安全的雙向通訊。Azure IoT Hub is a fully managed service that enables reliable and secure bidirectional communications between millions of IoT devices and a solution back end. Azure IoT 中樞:Azure IoT Hub:

  • 提供多個裝置到雲端和雲端到裝置通訊選項,包括單向傳訊、檔案傳輸,以及要求-回覆方法。Provides multiple device-to-cloud and cloud-to-device communication options, including one-way messaging, file transfer, and request-reply methods.
  • 向其他 Azure 服務提供內建宣告式訊息路由。Provides built-in declarative message routing to other Azure services.
  • 針對裝置中繼資料與同步化狀態資訊提供可查詢存放區。Provides a queryable store for device metadata and synchronized state information.
  • 使用每一裝置的安全性金鑰或 X.509 憑證啟用安全通訊與存取控制。Enables secure communications and access control using per-device security keys or X.509 certificates.
  • 可廣泛監視裝置的連線情況和裝置的身分識別管理事件。Provides extensive monitoring for device connectivity and device identity management events.
  • 包括適用於最受歡迎的語言和平台的裝置程式庫。Includes device libraries for the most popular languages and platforms.

使用 npm 來安裝 Node.js 的 Azure IoT 中樞模組Use npm to install the Azure IoT Hub modules for Node.js

管理套件Management Package

安裝 npm 模組Install the npm module

安裝 Azure IoT 中樞 npm 模組Install the Azure IoT Hub npm module

npm install azure-arm-iothub

範例Example

此範例會建立並命名 IoT 中樞。This example creates and names an IoT hub.

const msRestAzure = require('ms-rest-azure');
const IoTHubClient = require('azure-arm-iothub');

const subscriptionId = 'your-subscription-id';
const resourceGroup = 'your-resource-group';
const hubName = 'your-hub';
const location = 'East US';

// Describe the IoT hub you want to create
const hubDescription = {
  name: hubName,
  location: location,
  subscriptionid: subscriptionId,
  resourcegroup: resourceGroup,
  sku: { name: 'S1', capacity: 2 },
  properties: {
    enableFileUploadNotifications: false,
    ipFilterRules: [{ filterName: 'ipfilterrule', action: 'accept', ipMask: '0.0.0.0/0' }],
    operationsMonitoringProperties: {
      events: {
        C2DCommands: 'Error',
        DeviceTelemetry: 'Error',
        DeviceIdentityOperations: 'Error',
        Connections: 'Error, Information'
      }
    },
    features: 'None'
  }
};

msRestAzure.interactiveLogin().then(credentials => {
  const client = new IoTHubClient(credentials, subscriptionId);

  client.iotHubResource
    .createOrUpdate(resourceGroup, hubName, hubDescription)
    .then(hubDescriptionResult => console.log(hubDescriptionResult))
    .catch(err => console.error(err));
});

此範例會取得現有的 IoT 中樞中 (依名稱排列)。This example gets the existing IoT hub, by name.

const subscriptionId = 'your-subscription-id';
const resourceGroup = 'your-resource-group';
const hubName = 'your-hub';

msRestAzure.interactiveLogin().then(credentials => {
  const client = new IoTHubClient(credentials, subscriptionId);

  client.iotHubResource
    .get(resourceGroup, hubName)
    .then(hubDescriptionResult => console.log(hubDescriptionResult))
    .catch(err => console.error(err));
});

範例Samples

深入探索可在應用程式中使用的 Node.js 程式碼範例Explore more sample Node.js code you can use in your apps.