0

I am developing an Alexa Skill for the first time. For the Fulfillment sections, I planned to hook them up to serverless functions (written in Node.js) on Azure. I developed the intents with Google's Dialogflow which I plan to export to Amazon Alexa's console. I am a C# programmer but willing to learn Node.js/Javascript and have a few basic questions.

I installed/used the "azure-functions-core-tools" from Github to create my serverless function in Node.js. It created the file below.

The name of my Function is HelloWorld

Index.js file below was created by "func new" command and contains the following:

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    if (req.query.name || (req.body && req.body.name)) {
        context.res = {
            // status: 200, /* Defaults to 200 */
            body: "Hello " + (req.query.name || req.body.name)
        };
    }
    else {
        context.res = {
            status: 400,
            body: "Please pass a name on the query string or in the request body"
        };
    }
};

My questions are

1) Are you limited to one function per file?
2) Can I have multiple functions in the same file?
3) If so, how is that possible because just looking at this, there is no name for this function?
4) If not, how can I call other functions outside this function?
5) I am taking an online class on Node.js but wonder if I really should take a Javascript class instead. What would you recommend?

Many Thanks

PKonstant
  • 478
  • 1
  • 6
  • 21
  • 3
    You might want to hit some JS/NodeJS tutorials before going much further. Exports are just what the module exports; you can have anything else you want in the file, e.g., other functions. No way to know what NodeJS tutorial you're doing or how much JS it teaches. – Dave Newton Oct 05 '18 at 13:39
  • 1
    Your really should first learn the basic concepts of the languages you want to use. There are different way to work with functions [var functionName = function() {} vs function functionName() {}](https://stackoverflow.com/questions/336859), and how you can export so that you are able to use them outside of the file [Declare multiple module.exports in Node.js](https://stackoverflow.com/questions/16631064) – t.niese Oct 05 '18 at 13:47
  • For 3), how can that be, there is no name for this function? – PKonstant Oct 05 '18 at 14:17

1 Answers1

0

Answers:

  1. No
  2. Yes. You basically need to search for basic syntax using a search engine (example: google.com)
  3. Because some js-file/module will use this as a module, importing it using require and then using it as a method. For example :

-

// file name - run.js
var asyncFunction = require('index.js'); // This fetches the function, as its exported in index.js
asyncFunction() // this executes the function.
  1. You use export and require.
  2. Nodejs is a complete set of environment which uses javascript as the programming language. So it basically depends on the content of that course.
zeekhuge
  • 1,524
  • 12
  • 22
  • **2.** that's not a really useful information, I certain that the OP already knows that search engines exits. **3.** should be `require('index.js')`. **4.** giving some high level keywords and just linking to an external site that might change or go away at any time is not usefully to create a long lasting answer. – t.niese Oct 05 '18 at 13:51
  • @t.niese: Fixed 3 and 4 – zeekhuge Oct 05 '18 at 13:55
  • Why the use of "async" before the function? I thought Node.js is non-blocking out of the box - no need to do anything, unlike C#. – PKonstant Oct 05 '18 at 14:25
  • 1
    Makes me think the OP does not know about the search engine. @PKonstant [look here] [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function] (it needs a bit reading about Promises etc) – zeekhuge Oct 05 '18 at 14:28