Executing nodejs proxy error-Script exited with status code 0

Not applicable

I am trying to run a nodejs proxy that uses 'nock' module for creating a mock response as follows:

hello-world.js

var nock = require("nock");
var http = require("http");
var api = nock("http://javascriptplayground.com")
          .persist()
          .get("/test/")
          .reply(200, "Hello World");
http.get("http://javascriptplayground.com/test/", function(resp) {
  var str = "";
  resp.on("data", function(data) { str += data; });
  resp.on("end", function() {
    console.log("Got Result: ", str);
  });
});

I have installed the 'nock' module:

package.json

{
  "name": "apigee-nock-mock",
  "version": "1.0.0",
  "description": "This is an example of an Apigee API Proxy leveraging mock",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "nock": "^0.59.1"
  }
}

When i run the proxy it gives 404 error and logs gives "*** Script hello-world.js exited with status code 0" error.

Any help will be appreciated.

0 1 511
1 REPLY 1

Yes, from my reading of your code, it runs, and then exits.

Using nodejs as a "target" within Apigee Edge requires the nodejs app to start a server. to listen for incoming requests. For a simple example, in the Apigee documentation, there is this sample:

var http = require('http');
console.log('node.js application starting...');
var svr = http.createServer(function(req, resp) {
    resp.end('Hello, Node!');
});

svr.listen(process.env.PORT || 9000, function() {
    console.log('Node HTTP server is listening');
});

You can see the call to http.createServer(). That means the nodejs code will continue to run, waiting for inbound requests. Your example code doesn't do that or anything similar.

Your code successfully completes, and exits. That's not going to work as a target of a proxy.

I think you need something shaped more like this:

// nockServer.js
// ------------------------------------------------------------------

var nock = require("nock");
var http = require("http");
var api = nock("http://javascriptplayground.com")
  .persist()
  .get("/test/")
  .reply(200, "Hello World");

function inboundRequestHandler(req, outboundResp) {
  http.get("http://javascriptplayground.com/test/", function(resp) {
    var str = "";
    resp.on("data", function(data) { str += data; });
    resp.on("end", function() {
      console.log("Got Result: ", str);
      outboundResp.end(str);
    });
  });
}

var svr = http.createServer(inboundRequestHandler);

svr.listen(process.env.PORT || 9000, function() {
    console.log('Node HTTP server is listening');
});

What's happening in that code?

the nock module is used to set up mock responses .... specifically for GET requests that are sent *outbound* to JavaScriptplayground.com .

The http module is used to create a server. The handler for inbound requests on that server, is a function. That function sends a GET to JavaScriptplayground.com . We expect that request to be handled by the nock "mock".

the server then starts listening.

In theory when I configure this code as the nodejs target in an Apigee Edge proxy, for every request, I will receive a mocked response "Hello World". Javascriptplayground.com is never contacted, because of the use of nock.

I say "in theory" because I've never used the nock module within trireme. I suppose it should work but I don't know. I haven't tested it. also I don't know the nock module and I'm not sure I'm doing it quite right. Finally, I don't see what the point of this would be, as a nodejs target in Apigee Edge.

good luck!