在 Node.js 中存取流程變數

查看 Apigee Edge 說明文件。
前往 Apigee X說明文件
資訊

簡介

使用 apigee-access 模組存取 Apigee Edge 流程 變數。模組包含取得設定刪除變數的方法。這也是設定整數變數的便利方法。

流程變數存在於 API Proxy 流程中。有些變數為「內建」到 邊緣有些則是在執行政策時建立,您也可以自行建立變數。流量 變數通常用於在政策之間傳遞資料,以及設定條件 納入條件式流程中如要瞭解流程變數,請參閱「流程變數和 限制條件

如需 apigee-access 模組和其其他功能的簡介,請參閱 使用 apigee-access 模組

實際範例

假設在要求流程路徑上執行的 Edge 政策設定了一個名為 AuthenticatedUserId。以下程式碼會存取該變數,並將其輸出到 。此外,此程式碼也會設定變數。接著您可以透過政策存取該變數 如下所示

var http = require('http');
var apigee = require('apigee-access');

http.createServer(function (request, response) {
  // The request parameter must be a request object that came from the http module
  var userId = apigee.getVariable(request, 'AuthenticatedUserId');
  apigee.setVariable(request, "custom.foo", "Bar");
  console.log('Authenticated Apigee User ID is %s', userId);
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World\n');
}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');

您可以將此程式碼複製到 JavaScript 檔案,然後部署至 Edge,然後試用。在 檔案 server.js。如要部署,請使用:

apigeetool deploynodeapp -u username -p password -o myorg -e test -n access -d . -m server.js -b /access
敬上

將應用程式部署至 Edge 後,請按照下列方式新增 AssignMessage 政策 ProxyEndpoint 要求流程:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AddUserId">
    <DisplayName>AddUserId</DisplayName>
    <FaultRules/>
    <Properties/>  
    <AssignVariable>
        <Name>AuthenticatedUserId</Name>
        <Value>ntesla</Value>
        <Ref/>
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

接著,將其他 AssignMessage 政策附加至目標端點回應預先流程:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="SetHeader">
    <DisplayName>SetHeader</DisplayName>
    <FaultRules/>
    <Properties/>
    <Set>
        <Headers>
            <Header name="MySpecialHeader">{custom.foo}</Header>
        </Headers>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

呼叫 Proxy 的方式如下:

curl -i http://myorg-test.apigee.net/access

接著前往管理 UI 的存取 Proxy 頁面,然後開啟 「開發」檢視畫面。按一下 [Node.js Logs] 即可查看記錄檔輸出內容。 。如果 Proxy 設定正確,您會看到 userId 變數已設定完成。 您也會在終端機視窗的 cURL 輸出內容中看到標頭已設定:

HTTP/1.1 200 OK

Content-Type: text/plain
Date: Tue, 27 05 2014 23:20:52 GMT
MySpecialHeader: Bar
Content-Length: 12
Connection: keep-alive

方法


getVariable

var result = getVariable(httpRequest, name);

取得具名變數。

參數:

  • httpRequest:來自 http 模組的要求物件。
  • name:(字串) 要擷取的變數名稱。

傳回:

字串或數字,取決於使用 setVariable() 設定的類型, 或由您的政策所建立。如果您存取其中一個 立即可用的 Edge 變數,您可以在變數參考資料中找到類型清單。適用於變數類型 請參閱特定政策的參考主題。

例如:

var apigee = require('apigee-access');
    // "httpRequest" must be a request object that came from the http module
    var val1 = apigee.getVariable(request, 'TestVariable');
    var val2 = apigee.getVariable(request, 'request.client.ip');

setVariable

setVariable(httpRequest, name, value);

設定變數。部分變數屬於唯讀性質,且 setVariable() 方法會擲回 如果您嘗試設定其中一個參數,就會發生這種情況如要判斷哪些變數是唯讀變數,請參閱 變數 參考資料

參數:

  • httpRequest:來自 http 模組的要求物件。
  • name:(字串) 要擷取的變數名稱。
  • value:可以是數字、字串、布林值、空值或未定義。

範例:

var apigee = require('apigee-access');
    apigee.setVariable(request, 'TestVariable', 'bar');
    // This will throw an exception because client.ip is read-only.
    apigee.setVariable(request, 'client.ip');
    

setIntVariable

setIntVariable(httpRequest, name, value);

setIntVariable() 方法是一種便利方法,會先將值參數強制轉換為 然後加以設定

參數:

  • httpRequest:來自 http 模組的要求物件。
  • name:(字串) 要設定的變數名稱。
  • value:值參數必須是字串或數字。

範例:

var apigee = require('apigee-access');
// Convert "123" to an integer and set it
apigee.setIntVariable(request, 'TestVariable', '123');
// Use something that's already a number
apigee.setIntVariable(request, 'TestVariable2', 42);

deleteVariable

刪除具名變數。刪除唯讀變數會發生錯誤。如需完整的 唯讀變數清單,請參閱變數參考資料

deleteVariable(httpRequest, name);

參數:

  • httpRequest:來自 http 模組的要求物件。
  • name:(字串) 要刪除的變數名稱。

範例:

  apigee.deleteVariable(request, 'TestVariable');
    // This will throw an exception because client.ip is a read-only variable.
    apigee.deleteVariable(request, 'client.ip');