共用方式為


跨因特網模擬 CRUD API

開發 Proxy 可讓您 模擬 CRUD API ,而不需要建置 API。 使用 Dev Proxy 模擬 API 可讓您節省時間並加速開發。 在整合 API 與雲端服務時,您必須跨因特網公開 API,讓雲端服務可以存取它。 若要在因特網上公開由 Dev Proxy 仿真的 CRUD API,請使用 開發通道。 本文說明如何使用開發通道,設定要透過因特網公開 CRUD API。

提示

本文中的 CRUD API 是以 Northwind 資料庫開發 Proxy 範例為基礎。

設定要透過因特網公開的 CRUD API

若要在因特網上公開開發人員 Proxy 仿真的 CRUD API,請先設定 CRUD API。

重要

目前,您只能使用開發通道跨因特網公開 HTTP CRUD API。

定義 CRUD API 資料

Create 名為orders-data.json的數據檔,其支援 CRUD API,例如:

[
  {
    "OrderID": 10248,
    "CustomerID": "VINET",
    "EmployeeID": 5,
    "OrderDate": "1996-07-04T00:00:00",
    "RequiredDate": "1996-08-01T00:00:00",
    "ShippedDate": "1996-07-16T00:00:00",
    "ShipVia": 3,
    "Freight": 32.38,
    "ShipName": "Vins et alcools Chevalier",
    "ShipAddress": "59 rue de l'Abbaye",
    "ShipCity": "Reims",
    "ShipPostalCode": "51100",
    "ShipCountry": "France"
  },
  {
    "OrderID": 10249,
    "CustomerID": "TOMSP",
    "EmployeeID": 6,
    "OrderDate": "1996-07-05T00:00:00",
    "RequiredDate": "1996-08-16T00:00:00",
    "ShippedDate": "1996-07-10T00:00:00",
    "ShipVia": 1,
    "Freight": 11.61,
    "ShipName": "Toms Spezialitäten",
    "ShipAddress": "Luisenstr. 48",
    "ShipCity": "Münster",
    "ShipPostalCode": "44087",
    "ShipCountry": "Germany"
  }
]

設定 CRUD API

接下來,建立名為 orders-api.json的 API 組態檔,您可以在其中指定 CRUD API URL、其作業和數據檔。 請務必在 屬性中 baseUrl 指定 HTTP URL:

{
  "$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v0.17.0/crudapiplugin.schema.json",
  "baseUrl": "http://api.northwind.com/orders",
  "auth": "none",
  "dataFile": "orders-data.json",
  "actions": [
    {
      "action": "getAll"
    },
    {
      "action": "getOne",
      "url": "/{order-id}",
      "query": "$.[?(@.OrderID == {order-id})]"
    },
    {
      "action": "create"
    },
    {
      "action": "merge",
      "url": "/{order-id}",
      "query": "$.[?(@.OrderID == {order-id})]"
    },
    {
      "action": "delete",
      "url": "/{order-id}",
      "query": "$.[?(@.OrderID == {order-id})]"
    }
  ]
}

定義開發 Proxy 組態

接下來,建立名為 且devproxyrc.jsonCrudApiPlugin已啟用的 Dev Proxy 組態檔。 設定 Dev Proxy 以接聽您為 CRUD API 設定的 URL:

{
  "$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v0.17.0/rc.schema.json",
  "plugins": [
    {
      "name": "CrudApiPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
      "configSection": "ordersApi"
    }
  ],
  "urlsToWatch": [
    "http://api.northwind.com/*"
  ],
  "ordersApi": {
    "apiFile": "orders-api.json"
  }
}

驗證組態

執行 Dev Proxy 並將要求傳送至 CRUD API,以確認 CRUD API 正常運作。

啟動 Dev Proxy,假設您已將 Dev Proxy 組態儲存在目前工作目錄中名為 devproxyrc.json 的檔案中:

devproxy

使用 curl 呼叫 CRUD API:

$ curl -x http://127.0.0.1:8000 http://api.northwind.com/orders

[
  {
    "OrderID": 10248,
    "CustomerID": "VINET",
    "EmployeeID": 5,
    "OrderDate": "1996-07-04T00:00:00",
    "RequiredDate": "1996-08-01T00:00:00",
    "ShippedDate": "1996-07-16T00:00:00",
    "ShipVia": 3,
    "Freight": 32.38,
    "ShipName": "Vins et alcools Chevalier",
    "ShipAddress": "59 rue de l'Abbaye",
    "ShipCity": "Reims",
    "ShipPostalCode": "51100",
    "ShipCountry": "France"
  },
  {
    "OrderID": 10249,
    "CustomerID": "TOMSP",
    "EmployeeID": 6,
    "OrderDate": "1996-07-05T00:00:00",
    "RequiredDate": "1996-08-16T00:00:00",
    "ShippedDate": "1996-07-10T00:00:00",
    "ShipVia": 1,
    "Freight": 11.61,
    "ShipName": "Toms Spezialitäten",
    "ShipAddress": "Luisenstr. 48",
    "ShipCity": "Münster",
    "ShipPostalCode": "44087",
    "ShipCountry": "Germany"
  }
]

跨因特網公開 CRUD API

若要跨因特網公開 CRUD API,請啟動對應至開發 Proxy 埠的開發通道。 設定通道以使用針對 CRUD API 設定的主機名。

警告

允許匿名存取開發通道表示因特網上的任何人都可以連線到您的本機伺服器,如果他們可以猜測開發通道標識碼。

$ devtunnel host -p 8000 -a --host-header api.northwind.com

Hosting port: 8000
Connect via browser: https://vpfm55qw.euw.devtunnels.ms:8000, https://vpfm55qw-8000.euw.devtunnels.ms
Inspect network activity: https://vpfm55qw-8000-inspect.euw.devtunnels.ms

Ready to accept connections for tunnel: vpfm55qw

使用 curl 呼叫 Dev Proxy 透過開發通道仿真的 CRUD API:

$ curl https://vpfm55qw-8000.euw.devtunnels.ms/orders

[
  {
    "OrderID": 10248,
    "CustomerID": "VINET",
    "EmployeeID": 5,
    "OrderDate": "1996-07-04T00:00:00",
    "RequiredDate": "1996-08-01T00:00:00",
    "ShippedDate": "1996-07-16T00:00:00",
    "ShipVia": 3,
    "Freight": 32.38,
    "ShipName": "Vins et alcools Chevalier",
    "ShipAddress": "59 rue de l'Abbaye",
    "ShipCity": "Reims",
    "ShipPostalCode": "51100",
    "ShipCountry": "France"
  },
  {
    "OrderID": 10249,
    "CustomerID": "TOMSP",
    "EmployeeID": 6,
    "OrderDate": "1996-07-05T00:00:00",
    "RequiredDate": "1996-08-16T00:00:00",
    "ShippedDate": "1996-07-10T00:00:00",
    "ShipVia": 1,
    "Freight": 11.61,
    "ShipName": "Toms Spezialitäten",
    "ShipAddress": "Luisenstr. 48",
    "ShipCity": "Münster",
    "ShipPostalCode": "44087",
    "ShipCountry": "Germany"
  }
]