Node.js에서 흐름 변수에 액세스

Apigee Edge 문서입니다.
Go to the Apigee X 문서로 이동합니다.
info

소개

Node.js 애플리케이션에서 apigee-access 모듈을 사용하여 Apigee Edge 흐름 변수 에 액세스합니다. 이 모듈에는 변수를 가져오고, 설정하고, 삭제하는 메서드가 있습니다. 또한 정수 변수를 설정하는 편의 메서드도 있습니다.

흐름 변수는 API 프록시 흐름 컨텍스트 내에 존재합니다. 일부 변수는 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/');

이 코드를 자바스크립트 파일에 복사하고 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 정책을 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>

다음과 같이 프록시를 호출할 수 있습니다.

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

이제 관리 UI에서 액세스 프록시의 페이지로 이동하여 개발 뷰를 가져옵니다. Node.js 로그를 클릭하여 프록시의 로그 출력을 확인합니다. 프록시가 올바르게 구성된 경우 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: 숫자, 문자열, 불리언, null 또는 정의되지 않은 값일 수 있습니다.

예:

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: 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');