Skip to content

Commit c25767c

Browse files
authored
Merge pull request #77506 from rh-max/srvls-nodejs-primitive-types
[SRVOCF-513] Add primitve types returning to Node.js function docs
2 parents 3f760e5 + c3ce14b commit c25767c

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

modules/serverless-nodejs-function-return-values.adoc

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,98 @@ function handle(context, customer) {
2222
}
2323
----
2424

25+
[id="serverless-nodejs-function-return-values-primitive-types_{context}"]
26+
== Returning primitive types
27+
28+
Functions can return any valid JavaScript type, including primitives such as strings, numbers, and booleans:
29+
30+
.Example function returning a string
31+
[source,javascript]
32+
----
33+
function handle(context) {
34+
return "This function Works!"
35+
}
36+
----
37+
38+
Calling this function returns the following string:
39+
40+
[source,terminal]
41+
----
42+
$ curl https://myfunction.example.com
43+
----
44+
45+
[source,terminal]
46+
----
47+
This function Works!
48+
----
49+
50+
.Example function returning a number
51+
[source,javascript]
52+
----
53+
function handle(context) {
54+
let somenumber = 100
55+
return { body: somenumber }
56+
}
57+
----
58+
59+
Calling this function returns the following number:
60+
61+
[source,terminal]
62+
----
63+
$ curl https://myfunction.example.com
64+
----
65+
66+
[source,terminal]
67+
----
68+
100
69+
----
70+
71+
.Example function returning a boolean
72+
[source,javascript]
73+
----
74+
function handle(context) {
75+
let someboolean = false
76+
return { body: someboolean }
77+
}
78+
----
79+
80+
Calling this function returns the following boolean:
81+
82+
[source,terminal]
83+
----
84+
$ curl https://myfunction.example.com
85+
----
86+
87+
[source,terminal]
88+
----
89+
false
90+
----
91+
92+
Returning primitives directly without wrapping them in an object results in a `204 No Content` status code with an empty body:
93+
94+
.Example function returning a primitive directly
95+
[source,javascript]
96+
----
97+
function handle(context) {
98+
let someboolean = false
99+
return someboolean
100+
}
101+
----
102+
103+
Calling this function returns the following:
104+
105+
[source,terminal]
106+
----
107+
$ http :8080
108+
----
109+
110+
[source,terminal]
111+
----
112+
HTTP/1.1 204 No Content
113+
Connection: keep-alive
114+
...
115+
----
116+
25117
[id="serverless-nodejs-function-return-values-headers_{context}"]
26118
== Returning headers
27119

0 commit comments

Comments
 (0)