Skip to content

Commit dc57c7a

Browse files
authored
add comments to nodejs sample
1 parent 207106f commit dc57c7a

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

  • helloworld-samples/function-codebundle-nodejs
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,69 @@
1+
// ======================================================= //
2+
// IBM Cloud Code Engine - Functions-as-a-Service Sample //
3+
// //
4+
// main.js (NodeJS sample) //
5+
// //
6+
// This sample code uses an external module "lorem-ipsum" //
7+
// to generate an arbitrary result message. IBM code //
8+
// engine functions code with references to external //
9+
// modules have to be deployed as code-bundles. //
10+
// //
11+
// This sample shows how an external reference is coded //
12+
// in the source file (main.js) and how the module is //
13+
// referenced in the packages.json file //
14+
// ======================================================= //
15+
16+
/**
17+
* The `main` function is the entry-point into the function.
18+
*
19+
* A Code engine function source file can define multiple
20+
* functions, but it needs to have one dedicated 'main'
21+
* function, which will be called by the code engine runtime
22+
* on function's invocation.
23+
*
24+
* The 'main' function has one optional argument, which
25+
* carries all the parameters the function was invoked with.
26+
* But in this example the input argument is not used.
27+
*
28+
* Nodejs allow to reference external modules by using
29+
* the require(...) or the import(...) call. See NodeJs
30+
* language documentation for more details.
31+
*/
132
function main(args) {
33+
34+
// refernce external module
235
const LoremIpsum = require("lorem-ipsum").LoremIpsum;
36+
// create a default text generator.
337
const lorem = new LoremIpsum();
438

39+
// finally, build an HTML response that can be rendered
40+
// properly in a browser. To do so, we need to specify
41+
// the correct 'Content-Type' ('text/html' for HTML markup)
42+
// Alternatively, we could also use 'text/plain' or
43+
// 'application/json', depending on the text we plan to
44+
// return.
545
return {
46+
// specify headers for the HTTP response
47+
// we only set the Content-Type in this case, to
48+
// ensure the text is properly displayed in the browser
649
headers: { "Content-Type": "text/plain;charset=utf-8" },
50+
// use the text generator to create a response sentence
51+
// with 10 words
752
body: lorem.generateWords(10),
853
};
954
}
1055

56+
// export the main function within this source file
57+
// as the 'main' symbol to make it known to the runtime
58+
// This also works, if the function name is not 'main'.
59+
// By exporting the function as 'main', it will be
60+
// found and invoked by the runtime.
61+
//
62+
// Example:
63+
// function my_main_func_with_another_name(params) {
64+
// ...
65+
// }
66+
// ...
67+
// // this also works!
68+
// module.exports.main = my_main_func_with_another_name
1169
module.exports.main = main;

0 commit comments

Comments
 (0)