您目前查看的是 Apigee Edge 說明文件。
前往 Apigee X 說明文件。 info
簡介
在 Node.js 應用程式中,使用 apigee-access 模組存取 Apigee Edge 流程變數。這個模組提供取得、設定及刪除變數的方法。此外,這個類別也提供設定整數變數的便利方法。
流程變數存在於 API Proxy 流程的環境中。部分變數是 Edge「內建」的,有些變數會在政策執行時建立,您也可以建立自己的變數。流程變數通常用於在不同政策之間傳遞資料,以及在條件流程中設定條件。如要瞭解流程變數,請參閱「流程變數和條件」。
如要瞭解 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 後,請在 ProxyEndpoint 要求流程中新增 AssignMessage 政策,並進行下列設定:
<?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 政策附加至 TargetEndpoint 回應預先流程:
<?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」(Node.js 記錄),即可查看 Proxy 的記錄輸出內容。如果 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() 方法是便利方法,會先將 value 參數強制轉型為整數,然後設定該整數。
參數:
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');