<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Testen and debugging Node.js in API function]]></title><description><![CDATA[<p dir="auto">What is the best way to test and debug Node.js in an API function? I now create a test.html page that calls the api function and throw in a few console.log()'s in the code ... but there has to be an better/easier way ... I hope <img src="https://community.appdrag.com/plugins/nodebb-plugin-emoji/emoji/android/1f609.png?v=lfk1vimvd74" class="not-responsive emoji emoji-android emoji--wink" title=";-)" alt="😉" /></p>
]]></description><link>https://community.appdrag.com/topic/602/testen-and-debugging-node-js-in-api-function</link><generator>RSS for Node</generator><lastBuildDate>Tue, 15 Sep 2026 00:13:32 GMT</lastBuildDate><atom:link href="https://community.appdrag.com/topic/602.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 22 Dec 2020 13:43:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Testen and debugging Node.js in API function on Wed, 23 Dec 2020 07:34:34 GMT]]></title><description><![CDATA[<p dir="auto">To write and test your lambda locally you can also use Lambda Local (<a href="https://www.npmjs.com/package/lambda-local" rel="nofollow ugc">https://www.npmjs.com/package/lambda-local</a>)</p>
<p dir="auto">This would allow you to write different tests with different parameters.</p>
<p dir="auto">Could be called through Mocha (<a href="https://www.npmjs.com/package/mocha" rel="nofollow ugc">https://www.npmjs.com/package/mocha</a><br />
) tests</p>
<p dir="auto">The whole embeded in an <strong>npm deploy script</strong> that would test it and if all tests go well call the publish API function from AppDrag CLI (<a href="https://www.npmjs.com/package/appdrag" rel="nofollow ugc">https://www.npmjs.com/package/appdrag</a>)</p>
]]></description><link>https://community.appdrag.com/post/2363</link><guid isPermaLink="true">https://community.appdrag.com/post/2363</guid><dc:creator><![CDATA[Wassim]]></dc:creator><pubDate>Wed, 23 Dec 2020 07:34:34 GMT</pubDate></item><item><title><![CDATA[Reply to Testen and debugging Node.js in API function on Tue, 22 Dec 2020 21:05:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://community.appdrag.com/uid/9">@Daniel-Mulroy</a> thanks for the info. I'm just getting to know JavaScript and understand a bit of how Node.js is working, but haven't got the time to look at VS Code (yet). But when I do, I'll revert back to your instructions <img src="https://community.appdrag.com/plugins/nodebb-plugin-emoji/emoji/android/1f642.png?v=lfk1vimvd74" class="not-responsive emoji emoji-android emoji--slightly_smiling_face" title=":-)" alt="🙂" /> Thanks!</p>
]]></description><link>https://community.appdrag.com/post/2355</link><guid isPermaLink="true">https://community.appdrag.com/post/2355</guid><dc:creator><![CDATA[Dick Honing]]></dc:creator><pubDate>Tue, 22 Dec 2020 21:05:01 GMT</pubDate></item><item><title><![CDATA[Reply to Testen and debugging Node.js in API function on Tue, 22 Dec 2020 16:33:04 GMT]]></title><description><![CDATA[<p dir="auto">I enjoy writing code locally in VS code. You can use a debugger to step through the code and see what's happening 'in between lines'.</p>
<p dir="auto">Then, when it's ready, you can put it in an AppDrag function.</p>
<p dir="auto">However.... because the Node functions are lambda functions, they are called by an external service (a handler).</p>
<p dir="auto">What I do is I have a file called "testFunction.js" like this:</p>
<pre><code>//import your handler file or main file of Lambda
const request = require("request-promise-native");
const envVars = {
    APIKEY : "your-appdrag-api-key",
    APPID : "your-appdrag-app-id"
}
Object.assign(process.env, envVars);
let fn = require(`./${process.env.file}`);

console.log("Starting up... " + process.env.file)

process.on('beforeExit', (code) =&gt; {
  console.log('Process beforeExit event with code: ', code);
});

process.on('exit', (code) =&gt; {
  console.log('Process exit event with code: ', code);
});

//Call your exports function with required params
//In AWS lambda these are event, content, and callback
//event and content are JSON object and callback is a function
//In my example i'm using empty JSON
fn.handler({
    "POST": {
"somevar" : "Goes here",
    },
    "HEADERS": {ip: "0.0.0.0", "User-Agent": "LOCAL TESTING"}
  }, //event
  function(data, ss) { //callback function with two arguments
    console.log(data);
    console.log(ss);
  });
</code></pre>
<p dir="auto">Then I copy/paste the code from my AppDrag function (or use the cli tool to download it) and save it locally, for example as myfunction.js</p>
<p dir="auto">Lastly, from the command line, (you need to have a similar version of node installed), you can run:</p>
<pre><code>file="myfunction.js" node testFunction.js
</code></pre>
<p dir="auto">That passes 'myfunction.js' to your testFunction.js as a parameter, and it calls it as if it were being called as a lambda invocation.</p>
<p dir="auto">I'm definitely no lambda expert, and some of my terminology may be a little fishy, but that's how I test most of my code 'offline'.</p>
]]></description><link>https://community.appdrag.com/post/2351</link><guid isPermaLink="true">https://community.appdrag.com/post/2351</guid><dc:creator><![CDATA[Daniel Mulroy]]></dc:creator><pubDate>Tue, 22 Dec 2020 16:33:04 GMT</pubDate></item></channel></rss>