Environment for node.js

Not applicable

It is possible to set environment variables and arguments for node.js scripts in the ScriptTarget tag. It also appears that some environment variables may already be defined, but I cannot find any documentation on the node.js environment. What environment variables are set when a node.js script starts running? Is it possible to determine the environment (i.e., test/prod/...) and organization before a request comes into the node.js application?

Solved Solved
1 9 1,210
1 ACCEPTED SOLUTION

@Eric Hildum - As you may already know, this is the documentation page that talks about how to set environment variables and arguments to the node.js process: http://apigee.com/docs/api-services/content/understanding-edge-support-nodejs-modules#advancedscript...

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

Q1 - What environment variables are set when a node.js script starts running?

You can easily find this out with this node.js code:

console.log('environment variables: ' + JSON.stringify(process.env));

When I execute this in my free org, I get:

{
  "TERM": "xterm",
  "JAVA_HOME": "/usr/lib/jvm/java-1.7.0-openjdk.x86_64",
  "SHLVL": "3",
  "XFILESEARCHPATH": "/usr/dt/app-defaults/%L/Dt",
  "APIGEE_HOME": "/opt/apigee",
  "microkernel_overrideTokenFolder": "/opt/apigee/token",
  "working_dir": "/opt/apigee/apigee-1.0.0.974.5491b78.1507171825/bin",
  "microkernel_host": "rmp69apigee",
  "PWD": "/",
  "microkernel_overrideTokenDirs": "application region pod org host",
  "LOGNAME": "apigee",
  "_APIGEE_APP_ID": "V+/lh17zvLtIy1TWFLWYv2zZxvWDlvFgMFMcCjglDfM=",
  "_": "/usr/lib/jvm/java-1.7.0-openjdk.x86_64/bin/java",
  "microkernel_defaultTokenFolder": "/opt/apigee/apigee-1.0.0.974.5491b78.1507171825/token",
  "NLSPATH": "/usr/dt/lib/nls/msg/%L/%N.cat",
  "microkernel_timeout": "T60",
  "SHELL": "/bin/bash",
  "TMPDIR": "/tmp"
  "microkernel_hasMonetization": "false",
  "USE_TOKENIZED_CONFIG": "true",
  "microkernel_org": "default",
  "PATH": "/sbin:/usr/sbin:/bin:/usr/bin",
  "microkernel_application": "message-processor",
  "microkernel_installType": "cloud",
  "microkernel_instanceType": "c3.xlarge",
  "USER": "apigee",
  "HOME": "/home/apigee",
  "APIGEE_ORGANIZATION": "oseymen",
  "CONFIG_UTIL_HOME": "/opt/apigee/apigee-1.0.0.974.5491b78.1507171825/bin",
  "microkernel_pod": "rea1gw012",
  "APIGEE_ENVIRONMENT": "test",
  "microkernel_defaultTokenDirs": "application installType:application instanceType timeout",
  "microkernel_region": "us-east-1",
  "LANG": "en_US.UTF-8"
}

Q2 - Is it possible to determine the environment (i.e., test/prod/...) and organization before a request comes into the node.js application?

I am not 100% on what you mean by "before request comes into node.js application", but here are a couple of pointers:

  • Using apigee-access module in node.js and read the value of apigee variable "environment.name" and "organization.name"
  • Reading environment variable "APIGEE_ENVIRONMENT" and "APIGEE_ORGANIZATION" in node.js - see above JSON object.
  • Passing the whatever you want as an environment variable in ScriptTarget and reading it in node.js
  • Passing the whatever you want as an argument and reading it from process.argv.
  • Passing the whatever you want as a header/qs, etc from Apigee policies to node.js and reading it from there.

View solution in original post

9 REPLIES 9

Not applicable

Hi @Eric Hildum ,I don't think the script target supports properties but you can acheive the same using the apigee-access module .

check this http://apigee.com/docs/api-services/content/using-apigee-access .

Not correct. See the bottom of http://apigee.com/docs/api-services/content/understanding-edge-support-nodejs-modules, where setting both arguments and environment variables are discussed. Note also that Apigee recommends that port parameters check the environment variable PORT, with 9000 as the default (higher up on the same page). So clearly there is a possibility that some environment variables will be set other than those in the ScriptTarget tag. The question is, what are they? It would be really helpful if both the organization and current environment name were set in environment variables that the script could check prior to a request coming in on a flow. It would allow for some housekeeping and consistency checks before requests come in.

I believe you are talking about the below at the end ?

svr.listen(process.env.PORT ||9000,function()

{ console.log('The server is running.');});

That part is ignored by Edge once you deploy the apiproxy with node.js , in fact you will not see anything running on port 9000 .Even if you try to set the port to something other than 9000 in your messageprocessor , you will not see anything listening .

The only reason I could think of that snippet is to allow the same code that you run on your standalone node.js scipt to run on Edge so that you don't need to change your script to run on Edge .

Regarding the environment variables or arguments in that page , I have never used them as I can get everything I wanted using the flow variables .

However you can use the env variables and arguments in the script target tag and access them in your script using process.env. * or process.args[*] just like how you do it in your node.js script . https://nodejs.org/api/process.html#process_process

Pls use keyvaluemaps to set any environment specific configurations and using apigee-access module you can access them in your node.js script too.

In your case you can use the out-of-box variable environment.name (more here http://apigee.com/docs/api-services/reference/variables-reference)

Pls check this page for more info on how to access flow variables using apigee-access http://apigee.com/docs/api-services/content/access-flow-variables-nodejs

Adding few other folks who can add or correct the above .

cc @Diego Zuluaga cc @Stephen Gilson

Added the text in bold above after little bit of research and talking to @Diego Zuluaga.

@Maruti Chand Sorry Maruti, I think you really missed what I was asking about, which is the environment outside the flow variables. While the flow variables are documented, the rest of the environment variable set are not.

Hi @Eric Hildum , Now I get what you were asking . I don't think it was documented in the public docs but I can see about it here http://apigee.com/docs/release-notes/content/140903-apigee-edge-cloud-release-notes in the release notes . Looks like they only work on 14.09 + , hope you are on latest release .

@Eric Hildum - As you may already know, this is the documentation page that talks about how to set environment variables and arguments to the node.js process: http://apigee.com/docs/api-services/content/understanding-edge-support-nodejs-modules#advancedscript...

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

Q1 - What environment variables are set when a node.js script starts running?

You can easily find this out with this node.js code:

console.log('environment variables: ' + JSON.stringify(process.env));

When I execute this in my free org, I get:

{
  "TERM": "xterm",
  "JAVA_HOME": "/usr/lib/jvm/java-1.7.0-openjdk.x86_64",
  "SHLVL": "3",
  "XFILESEARCHPATH": "/usr/dt/app-defaults/%L/Dt",
  "APIGEE_HOME": "/opt/apigee",
  "microkernel_overrideTokenFolder": "/opt/apigee/token",
  "working_dir": "/opt/apigee/apigee-1.0.0.974.5491b78.1507171825/bin",
  "microkernel_host": "rmp69apigee",
  "PWD": "/",
  "microkernel_overrideTokenDirs": "application region pod org host",
  "LOGNAME": "apigee",
  "_APIGEE_APP_ID": "V+/lh17zvLtIy1TWFLWYv2zZxvWDlvFgMFMcCjglDfM=",
  "_": "/usr/lib/jvm/java-1.7.0-openjdk.x86_64/bin/java",
  "microkernel_defaultTokenFolder": "/opt/apigee/apigee-1.0.0.974.5491b78.1507171825/token",
  "NLSPATH": "/usr/dt/lib/nls/msg/%L/%N.cat",
  "microkernel_timeout": "T60",
  "SHELL": "/bin/bash",
  "TMPDIR": "/tmp"
  "microkernel_hasMonetization": "false",
  "USE_TOKENIZED_CONFIG": "true",
  "microkernel_org": "default",
  "PATH": "/sbin:/usr/sbin:/bin:/usr/bin",
  "microkernel_application": "message-processor",
  "microkernel_installType": "cloud",
  "microkernel_instanceType": "c3.xlarge",
  "USER": "apigee",
  "HOME": "/home/apigee",
  "APIGEE_ORGANIZATION": "oseymen",
  "CONFIG_UTIL_HOME": "/opt/apigee/apigee-1.0.0.974.5491b78.1507171825/bin",
  "microkernel_pod": "rea1gw012",
  "APIGEE_ENVIRONMENT": "test",
  "microkernel_defaultTokenDirs": "application installType:application instanceType timeout",
  "microkernel_region": "us-east-1",
  "LANG": "en_US.UTF-8"
}

Q2 - Is it possible to determine the environment (i.e., test/prod/...) and organization before a request comes into the node.js application?

I am not 100% on what you mean by "before request comes into node.js application", but here are a couple of pointers:

  • Using apigee-access module in node.js and read the value of apigee variable "environment.name" and "organization.name"
  • Reading environment variable "APIGEE_ENVIRONMENT" and "APIGEE_ORGANIZATION" in node.js - see above JSON object.
  • Passing the whatever you want as an environment variable in ScriptTarget and reading it in node.js
  • Passing the whatever you want as an argument and reading it from process.argv.
  • Passing the whatever you want as a header/qs, etc from Apigee policies to node.js and reading it from there.

Perfect. This is exactly what I was looking for. So it is possible for a node application to know where it is deployed before any requests arrive. That will let me do my housekeeping easily. Wish this was officially documented - Apigee could change this out from under me at any time if they have not committed to the node environment in writing.

To clarify, as you may recall, when an apigee proxy is deployed, it immediately starts all included node applications. These are normally configured to create a server and start listening for requests. However, they are executing before requests come in, before the server is set up (obviously, node has to be running to set up the server). During this time, they have access to the arguments and environment, but no access to flow variables (hence apigee-access is unusable), and could be doing useful setup and configuration work.

Thanks @Eric Hildum for the feedback. Adding @Floyd Jones to consider this for "official" documentation.