Environment variables for Node.js app

Was going through this page and came across the below:

<TargetEndpoint name="default">
  <ScriptTarget>
     <ResourceURL>node://hello.js</ResourceURL>
     <EnvironmentVariables>
         <EnvironmentVariable name="NAME">VALUE</EnvironmentVariable> 
     </EnvironmentVariables>
     <Arguments>
         <Argument>ARG</Argument>
     </Arguments>
  </ScriptTarget>
</TargetEndpoint>

Here the VALUE cannot be dynamic?

I know, I can use apigee-access module (apigee.getVariable(req,"flow.variable")), to access these in Node. However, I do not always have access to the req parameter all the way down.

Is it possible to set dynamic arguments to Node application via the TargetEndpoint configuration?

In my use case, I would be looking up values on the KVM and these would be fetched by appropriate layers on Node. I do not have to rely on passing the req parameter all the way down, just to access the flow variable (via apigee-access). Any suggestions/alternatives on how to best achieve this?

Thanks,

Girish

Solved Solved
0 3 1,371
1 ACCEPTED SOLUTION

Not applicable

Dear @Girish Gajria, you're right about setting dynamic values from variables in Node.js as environment variables. Therefore, you won't be able to leverage them for the purpose of passing values dynamically. However, based on your use case, you can leverage many other features available in Apigee Edge to retrieve dynamic variables. I'll try to sumarize the ones I'm familiar with:

  • Setting a context variable and access it through apigee-access Node.js Module. It seems that you're already familiar with this option. However, based on your comments, this option doesn't suite your requirements, because the request object is not always available. My only comment to you is that since Node.js is always set as a Target in Edge, the request object should always be present. So, you should be good making this assumption.
  • Setting values through KVMs. This is okay. However, since KVMs aren't directly accessible at this point in Node.js, you'll still need to set the KVM as a variable to make it accessible.
  • Setting the value through Apigee Vault. It is perhaps the most native way to set a dynamic value since apigee-access already supports it.

Am I missing anything?

View solution in original post

3 REPLIES 3

Not applicable

Dear @Girish Gajria, you're right about setting dynamic values from variables in Node.js as environment variables. Therefore, you won't be able to leverage them for the purpose of passing values dynamically. However, based on your use case, you can leverage many other features available in Apigee Edge to retrieve dynamic variables. I'll try to sumarize the ones I'm familiar with:

  • Setting a context variable and access it through apigee-access Node.js Module. It seems that you're already familiar with this option. However, based on your comments, this option doesn't suite your requirements, because the request object is not always available. My only comment to you is that since Node.js is always set as a Target in Edge, the request object should always be present. So, you should be good making this assumption.
  • Setting values through KVMs. This is okay. However, since KVMs aren't directly accessible at this point in Node.js, you'll still need to set the KVM as a variable to make it accessible.
  • Setting the value through Apigee Vault. It is perhaps the most native way to set a dynamic value since apigee-access already supports it.

Am I missing anything?

Thanks @Diego Zuluaga. So I guess 1st option is the (only) best option. The only issue is due to the callback pattern of node.js, now I need to pass the req variable to all of the deepest functions. I was looking for a way to avoid this, oh well.

Thanks Again,

Girish

Essentially yes. But I know where you're coming from and I've run into similar scenarios. There maybe a few ways to workaround or reduce the dependency:

  1. Bind request object to the context instead of passing it as a parameter. This pattern is better explained by @Samwize is this article. http://samwize.com/2013/09/01/how-you-can-pass-a-variable-into-callback-function-in-node-dot-js/. And you're right, the dependency is still there, but it's perhaps cleaner than passing an unnecessary object to the function.
  2. Using call to bind request in the context. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call#Using...

All in all, if you follow MVC pattern, the request object should be part of the parameter of the controller, so at that point, I'd recommend getting the variable from the request.

Hope it helps!