This topic explains the simplest way to wrap a Node.js application in an API proxy using the management UI.

Introduction

Chances are, the first Node.js app you ever created was an HTTP server that responds to requests with "Hello World!" The simplest way to try out Node.js on Apigee Edge is to do something very similar. With just a few mouse clicks, you'll have a functioning, proxied Node.js HTTP server running on Edge. After that, you can use the code editor in the UI to modify the Node.js app, add additional Node.js files, add policies, and so on. 

Creating the sample Hello World! API proxy

This section explains how to create an Edge API proxy that interacts with a Node.js HTTP server. The Node.js HTTP server code is already written for you and is automatically deployed to Edge when you follow these simple steps.

If you have not done so, you need to set up an account on Apigee Edge before you can try out this example.

  1. Log in to the Apigee Edge management UI
  2. From the APIs menu, select API Proxies.
  3. In the API Proxies summary page, click +API Proxy.


     
  4. In the New API proxy dialog, select New Node.js
  5. From the Node.js Server Type options, select Node.js Sample "Hello World". (We'll discuss the other Node.js Server Type, the "Integrated API BaaS" sample later in this topic.)
  6. Provide a Display Name for the proxy. You can just enter Hello.
  7. Provide a Project Base Path. If you specified Hello as the display name, then the path /hello is populated automatically. 
  8. Add a version number /v1 to the path. API versioning is a best practice. The new Project Base Path is:

    /v1/hello
     
  9. Optionally, provide a Description.


     
  10. Click Build.

Edge builds the proxy. When the build completes, the hello proxy appears in the API Proxies summary page. Select API Proxies from the APIs menu to see the API Proxies summary page, shown below:

Invoking the hello proxy

When you call the hello proxy, the Node.js application executes automatically, responding with "Hello, World!". Note that unless you specified otherwise, the Node.js application is deployed to the environment called test. Here's the basic call using Curl (substitute your organization name for myorg). 

$ curl http://myorg-test.apigee.net/v1/hello
Hello, World!

Viewing and editing the Node.js code

Let's look at the Node.js code that was added to the API proxy. Go to the summary page for the Hello World proxy and click Develop.


This opens the Develop view which includes a code editor. You can edit the code there directly.

 

For example, change the response from Hello, World! to something else, like Hello, Node!, then click Save. The proxy is saved and redeployed.

Finally, re-invoke the proxy to verify the change:

$ curl http://myorg-test.apigee.net/v1/hello
Hello, Node!

Understanding the Integrated API BaaS sample

Now that you've seen the Hello World sample, let's briefly look at the other out-of-the-box selection: the Integrated API BaaS sample.

The basic steps for creating and invoking this sample are the same as the Hello World! sample. The API BaaS sample is a little more complex, however. 

The Integrated API BaaS sample integrates two Node.js frameworks to create a simple API service. One API call simply returns a string embedded in the code, and the other retrieves data from an external service and returns that data in the response.

The frameworks used in this sample are Express and Argo. Both are web application frameworks. Express is a web application framework that is very popular among Node.js developers. Argo is another web application framework that is an open source project sponsored by Apigee.

To create this sample proxy:

  • Follow the same steps for creating the Hello World! proxy, but choose the Node.js application type called Node.js Sample: "Integrated API BaaS" sample.

Here is the source code that is deployed with this proxy:

var argo = require('argo');
var express = require('express');

var app = express();

var proxy = argo()
  .target('https://api.usergrid.com')
  .build();

app.get('/hello', function(req, res) {
  res.send('Hello from Express');
});

app.all('*', proxy.run);
app.listen(3000);

In this code, Express is used to specify the GET call, which simply sends back text when the API is called with /hello. The Argo method executes as well. It returns data from the site https://api.usergrid.com. The build() method prepares Argo's request event listener. The listener is currently accessible by calling the run method on the object returned from the build method. 

If you call this proxy with the /hello path, it returns "Hello from Express!":

$ curl http://myorg-test.apigee.net/v1/data-service/hello
Hello from Express!

If you call this proxy without /hello, it runs the argo proxy, which grabs information from the api.usergrid.com site:

$ curl http://myorg-test.apigee.net/v1/data-service

You can read more about the Argo framework at its repository in GitHub.

More about running Node.js applications on Apigee Edge

Like all Node.js applications, Node.js applications running on Apigee Edge run in a single thread of control. There is no need (and in fact no ability) to start another thread, or synchronize variables between threads. Since Node.js enforces non-blocking programming, a single script can support thousands of concurrent requests because the script gives up the CPU whenever it has to wait for something, and it is notified later when it happens.
 
To use Node.js on Apigee Edge, you need to specify a main Node.js script file. This script must be configured to handle incoming requests, which you typically do by using the http or https modules, creating a client, and so on. (If the main script is not configured this way, it will simply execute and exit after it is deployed.) Within Apigee Edge, each Node.js application script is started from the beginning when the proxy is deployed, and stopped when the proxy is undeployed. In between it will wait for new requests and process them. For more information, see "Invoking an imported Node.js file" in Deploying a standalone Node.js app.

Next Steps

You can also create and deploy standalone Node.js applications directly from your file system. The next topic, Deploying a standalone Node.js app, explains how to use the apigeetool command to deploy a Node.js app from the command line.

 

 

Help or comments?

  • Something's not working: See Apigee Support
  • Something's wrong with the docs: Click Send Feedback in the lower right.
    (Incorrect? Unclear? Broken link? Typo?)