Reference a JS file from within NodeJS Target Endpoint

Not applicable

Hello - I built a NodeJS endpoint and I would like to separate some of the logic in that one large file into separate files. I have uploaded three node files and configured my xml:

<Resources>
  <Resource>node://target-endpoint.js</Resource>
  <Resource>node://reference-one.js</Resource>
  <Resource>node://reference-two.js</Resource>
</Resources>

And I configured my TargetEndpoint:

<TargetEndpoint name="default">
    ...
    <ScriptTarget>
        <ResourceURL>node://target-endpoint.js</ResourceURL>
    </ScriptTarget>
</TargetEndpoint>

From within target-endpoint.js I attempt to "include" the two reference files:

require('./reference-one');
require('./reference-two');

I have tried referencing it as './reference-one', './reference-one.js', and 'node://reference-one.js'.

I either get an error message that the module cannot be found (which tells me I am referencing wrong) OR I get the following message:

{"fault":{"detail":{"errorcode":"scripts.node.runtime.ScriptStillStarting"},"faultstring":"Script node is still starting"}}

How do I do what I want to do? Thanks!

0 7 1,004
7 REPLIES 7

Not applicable

Hi @GVoss,

I think you may be making this more complicated than necessary. If you handle the node.js setup via apigeetool, for example, then you'll end up with a proxy with a resources/node directory. Therein you can put any .js files you wish and reference them directly (see below).

If you don't want to handle it that way then you can simply upload them via the WEB-UI and store them as separate scripts and then reference them via a simple

var data      = require('./data.js');

where data.js is expected to lie next to the main script declared in your target endpoint.

Thanks,

/geir

Thanks Geir - I think that is what I have done. I created the two additional NodeJS files which live in /apiproxy/resource/node and am trying to reference them that way...

Not applicable

Hi @GVoss,

When I develop node.js bits I test them all locally and this works perfect. Is your node.js stuff working locally?

Also .. Can you u/l your proxy? I'll then have a look and see if I can spot the issue.

Thanks,

/geir

Not applicable

Edge is designed to run Node apps that run as HTTP targets. When you get the message, "Script node is still starting," it means that your app never created an HTTP server and called "listen" on it, so it can't accept any API calls. Does your app start an HTTP server?

Other than that, your use of "require" looks right.

@Greg Brail - the target endpoint creates an HTTP server and listens. All the logic started its life in the target endpoint JS and was working without any issues.

OK -- it sounds like your requires are correct, so it's not clear. Is there any more code that you can share with us?

My target endpoint NodeJS file looks like this

var util = require('util');
var querystring = require('querystring');
var http = require('http');
var https = require('https');
var async = require('async');
var apigeeCache = require('apigee-access');
require('./vdocuments.js');
require('./verrors.js');

http.createServer(function (request, response) {
  //Get the Auth Key
  //Call my API Endpoint
  //Return the result
}).listen(8124);//arbitrary port?

And my included NodeJS file(s) both look like this:

function SomeStructure(rawData)
{
  this.rawData = rawData;
};
  SomeStructure.prototype.getAttribute = function()
  {
    return this.rawData.attribute.length;
  };
  SomeStructure.prototype.getPayload = function()
  {
    var payload = {};
    this.rawData.errors.forEach(function(item)
    {
      payload.push(item);
    });
    return payload;
  };
  SomeStructure.prototype.getRawPayload = function()
  {
    return JSON.parse(this.rawData);
  };

I stripped out a lot of logic but I left the core things in there.