Skip to content

Commit 2f7c09b

Browse files
Merge pull request #184 from fnproject/mww-190814-node-notrig
Update node tutorial to use invoke endpoint instead of http trigger
2 parents 7353a93 + 20e1e02 commit 2f7c09b

File tree

1 file changed

+106
-103
lines changed

1 file changed

+106
-103
lines changed

node/intro/README.md

Lines changed: 106 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ your laptop, server, or cloud. In this introductory tutorial we'll walk through
44
developing a function using the JavaScript programming language and Node.js
55
(without installing any Node.js tools!) and deploying that function to a local
66
Fn server. We'll also learn about the core Fn concepts like applications and
7-
triggers.
7+
invoke endpoints.
88

99
### Before you Begin
1010
* Set aside about 15 minutes to complete this tutorial.
@@ -29,7 +29,7 @@ In the terminal type the following.
2929

3030
![user input](images/userinput.png)
3131
>```
32-
> fn init --runtime node --trigger http nodefn
32+
> fn init --runtime node nodefn
3333
>```
3434
3535
The output will be
@@ -43,9 +43,8 @@ func.yaml created.
4343
The `fn init` command creates a simple function with a bit of boilerplate to get
4444
you started. The `--runtime` option is used to indicate that the function we're
4545
going to develop will be written in Node. A number of other runtimes are also
46-
supported. The `--trigger` option creates an HTTP trigger for the function
47-
allowing you to invoke the function from a URL. Fn creates the simple function
48-
along with several supporting files in the `/nodefn` directory.
46+
supported. Fn creates the simple function along with several supporting files
47+
in the `/nodefn` directory.
4948

5049
### Review your Function File
5150

@@ -107,10 +106,6 @@ name: nodefn
107106
version: 0.0.1
108107
runtime: node
109108
entrypoint: node func.js
110-
triggers:
111-
- name: nodefn
112-
type: http
113-
source: /nodefn
114109
```
115110
116111
The generated `func.yaml` file contains metadata about your function and
@@ -123,7 +118,6 @@ declares a number of properties including:
123118
in `--runtime`.
124119
* entrypoint--the name of the Docker execution command to invoke when your function is called,
125120
in this case `node func.js`.
126-
* triggers--identifies the automatically generated trigger name and source. For example, this function would be requested from the URL `http://localhost:8080/t/appname/nodefn`. Where `appname` is the name of the app chosen for your function when it is deployed.
127121
128122
There are other user specifiable properties but these will suffice for
129123
this example. Note that the name of your function is taken from the containing
@@ -202,57 +196,34 @@ FN_REGISTRY: fndemouser
202196
Current Context: default
203197
Sending build context to Docker daemon 5.12kB
204198
Step 1/9 : FROM fnproject/node:dev as build-stage
205-
dev: Pulling from fnproject/node
206-
bdf0201b3a05: Already exists
207-
ed514f6061af: Already exists
208-
bc8940a76c7f: Already exists
209-
Digest: sha256:c77966d42f46328662978a68ecd5b3b3b26cf9e4c29a91c6064f07f59f97621a
210-
Status: Downloaded newer image for fnproject/node:dev
211199
---> b557a05fec78
212200
Step 2/9 : WORKDIR /function
213-
---> Running in 62a348f4d39b
214-
Removing intermediate container 62a348f4d39b
201+
---> Using cache
215202
---> ea3ae80dbec2
216203
Step 3/9 : ADD package.json /function/
204+
---> Using cache
217205
---> e7d319e022d2
218206
Step 4/9 : RUN npm install
219-
---> Running in c963c8b1f699
220-
npm notice created a lockfile as package-lock.json. You should commit this file.
221-
npm WARN [email protected] No repository field.
222-
223-
added 1 package from 2 contributors and audited 1 package in 0.935s
224-
found 0 vulnerabilities
225-
226-
Removing intermediate container c963c8b1f699
207+
---> Using cache
227208
---> bf5b6313c055
228209
Step 5/9 : FROM fnproject/node
229-
latest: Pulling from fnproject/node
230-
bdf0201b3a05: Already exists
231-
ed514f6061af: Already exists
232-
bc8940a76c7f: Already exists
233-
f93406d580f9: Already exists
234-
Digest: sha256:2ee8e9e4b1de3a29cde6fa957e3d7aaf6d2486f46926863e42af92c9d4a02404
235-
Status: Downloaded newer image for fnproject/node:latest
236210
---> c8da69259495
237211
Step 6/9 : WORKDIR /function
238-
---> Running in 128681aed76f
239-
Removing intermediate container 128681aed76f
212+
---> Using cache
240213
---> 1fe19c6ca66c
241214
Step 7/9 : ADD . /function/
242-
---> 3ca3886160d0
215+
---> 229045a70217
243216
Step 8/9 : COPY --from=build-stage /function/node_modules/ /function/node_modules/
244-
---> 48940744e184
217+
---> 59b17163fe37
245218
Step 9/9 : ENTRYPOINT ["node", "func.js"]
246-
---> Running in 50ac03eeaec7
247-
Removing intermediate container 50ac03eeaec7
248-
---> 82e19d55d014
249-
Successfully built 82e19d55d014
219+
---> Running in b0d793dd39bb
220+
Removing intermediate container b0d793dd39bb
221+
---> 7160364dec30
222+
Successfully built 7160364dec30
250223
Successfully tagged fndemouser/nodefn:0.0.2
251224
252225
Updating function nodefn using image fndemouser/nodefn:0.0.2...
253226
Successfully created function: nodefn with fndemouser/nodefn:0.0.2
254-
Successfully created trigger: nodefn
255-
Trigger Endpoint: http://localhost:8080/t/nodeapp/nodefn
256227
```
257228
258229
All the steps to load the current language Docker image are displayed.
@@ -270,64 +241,26 @@ let's us know that the function is packaged in the image
270241
271242
Note that the containing folder name 'nodefn' was used as the name of the
272243
generated Docker container and used as the name of the function that
273-
container was bound to. By convention it is also used for the trigger name.
244+
container was bound to.
274245
275246
Normally you deploy an application without the `--verbose` option. If you rerun the command a new image and version is created and loaded.
276247
277-
278-
## Invoke your Deployed Function
279-
280-
There are two ways to call your deployed function.
281-
282-
### Invoke with the CLI
283-
284-
The first is using the `fn` CLI which makes invoking your function relatively
285-
easy. Type the following:
286-
287-
![user input](images/userinput.png)
288-
>```sh
289-
> fn invoke nodeapp nodefn
290-
>```
291-
292-
which results in:
293-
294-
```js
295-
{"message":"Hello World"}
296-
```
297-
298-
When you invoked "nodeapp nodefn" the fn server looked up the
299-
"nodeapp" application and then looked for the Docker container image
300-
bound to the "nodefn" function and executed the code.
301-
302-
You can also pass data to the run command. Note that you set the content type for the data passed. For example:
303-
304-
![user input](images/userinput.png)
305-
>```sh
306-
> echo -n '{"name":"Bob"}' | fn invoke nodeapp nodefn --content-type application/json
307-
>```
308-
309-
```js
310-
{"message":"Hello Bob"}
311-
```
312-
313-
The JSON data was parsed and since `name` was set to "Bob", that value is passed
314-
in the output.
315-
316248
### Understand fn deploy
249+
317250
If you have used Docker before the output of `fn --verbose deploy` should look
318251
familiar--it looks like the output you see when running `docker build`
319252
with a Dockerfile. Of course this is exactly what's happening! When
320253
you deploy a function like this Fn is dynamically generating a Dockerfile
321254
for your function, building a container, and then loading it for execution.
322255
323-
> __NOTE__: Fn is actually using two images. The first contains the language compiler
324-
and all the necessary build tools. The second image packages all dependencies
325-
and any necessary language runtime components. Using this strategy, the final
326-
function image size can be kept as small as possible. Smaller Docker images are
327-
naturally faster to push and pull from a repository which improves overall
328-
performance. For more details on this technique see [Multi-Stage Docker Builds
329-
for Creating Tiny Go
330-
Images](https://medium.com/travis-on-docker/multi-stage-docker-builds-for-creating-tiny-go-images-e0e1867efe5a).
256+
> __NOTE__: Fn is actually using two images. The first contains the language
257+
> compiler and all the necessary build tools. The second image packages all
258+
> dependencies and any necessary language runtime components. Using this
259+
> strategy, the final function image size can be kept as small as possible.
260+
> Smaller Docker images are naturally faster to push and pull from a repository
261+
> which improves overall performance. For more details on this technique see
262+
> [Multi-Stage Docker Builds for Creating Tiny Go
263+
> Images](https://medium.com/travis-on-docker/multi-stage-docker-builds-for-creating-tiny-go-images-e0e1867efe5a).
331264
332265
When using `fn deploy --local`, fn server builds and packages your function
333266
into a container image which resides on your local machine.
@@ -365,33 +298,103 @@ NAME ID
365298
nodeapp 01DHHSP6X3NG8G00GZJ0000001
366299
```
367300
368-
We can also see the functions that are defined by an application. Since
369-
functions are exposed via triggers, the `fn list triggers <appname>` command
370-
is used. To list the functions included in "nodeapp" we can type:
301+
The `fn list functions <app-name>` command lists all the functions associated with and app.
371302
372303
![user input](images/userinput.png)
373304
>```sh
374-
> fn list triggers nodeapp
305+
> fn list functions nodeapp
375306
>```
376307
308+
The returns all the functions associated with the `nodeapp`.
309+
310+
```
311+
NAME IMAGE ID
312+
nodefn fndemouser/nodefn:0.0.2 01DJ900WFKNG8G00GZJ0000002
377313
```
378-
FUNCTION NAME ID TYPE SOURCE ENDPOINT
379-
nodefn nodefn 01DHHSPYT9NG8G00GZJ0000003 http /nodefn http://localhost:8080/t/nodeapp/nodefn
314+
315+
## Invoke your Deployed Function
316+
317+
There are two ways to call your deployed function.
318+
319+
### Invoke with the CLI
320+
321+
The first is using the `fn` CLI which makes invoking your function relatively
322+
easy. Type the following:
323+
324+
![user input](images/userinput.png)
325+
>```sh
326+
> fn invoke nodeapp nodefn
327+
>```
328+
329+
which results in:
330+
331+
```js
332+
{"message":"Hello World"}
333+
```
334+
335+
When you invoked "nodeapp nodefn" the fn server looked up the
336+
"nodeapp" application and then looked for the Docker container image
337+
bound to the "nodefn" function and executed the code.
338+
339+
You can also pass data to the run command. Note that you set the content type for the data passed. For example:
340+
341+
![user input](images/userinput.png)
342+
>```sh
343+
> echo -n '{"name":"Bob"}' | fn invoke nodeapp nodefn --content-type application/json
344+
>```
345+
346+
```js
347+
{"message":"Hello Bob"}
348+
```
349+
350+
The JSON data was parsed and since `name` was set to "Bob", that value is passed
351+
in the output.
352+
353+
354+
### Getting a Function's Invoke Endpoint
355+
356+
In addition to using the Fn `invoke` command, we can call a function by using a
357+
URL. To do this, we must get the function's invoke endpoint. Use the command
358+
`fn inspect function <appname> <function-name>`. To list the `nodefn` function's
359+
invoke endpoint we can type:
360+
361+
![user input](images/userinput.png)
362+
>```sh
363+
> fn inspect function nodeapp nodefn
364+
>```
365+
366+
```js
367+
{
368+
"annotations": {
369+
"fnproject.io/fn/invokeEndpoint": "http://localhost:8080/invoke/01DJ900WFKNG8G00GZJ0000002"
370+
},
371+
"app_id": "01DJ90077CNG8G00GZJ0000001",
372+
"created_at": "2019-08-14T21:19:58.451Z",
373+
"id": "01DJ900WFKNG8G00GZJ0000002",
374+
"idle_timeout": 30,
375+
"image": "fndemouser/nodefn:0.0.3",
376+
"memory": 128,
377+
"name": "nodefn",
378+
"timeout": 30,
379+
"updated_at": "2019-08-14T21:21:09.868Z"
380+
}
380381
```
381382
382-
The output confirms that nodeapp contains a `nodefn` function that can be called via this URL.
383+
The output confirms that `nodefn` functions invoke endpoint is:
384+
`http://localhost:8080/invoke/01DJ900WFKNG8G00GZJ0000002`. We can use this URL
385+
to call the function.
383386
384387
### Invoke with Curl
385388
386-
The other way to invoke your function is via HTTP. The Fn server exposes our
387-
deployed function at `http://localhost:8080/t/nodeapp/nodefn`, a URL
388-
that incorporates our application and function trigger as path elements.
389+
Once we have the invoke endpoint, the second method for invoking our function
390+
can be used, HTTP. The Fn server exposes our deployed function at
391+
`http://localhost:8080/invoke/01DJ900WFKNG8G00GZJ0000002`.
389392
390393
Use curl to invoke the function:
391394
392395
![user input](images/userinput.png)
393396
>```sh
394-
> curl -H "Content-Type: application/json" http://localhost:8080/t/nodeapp/nodefn
397+
> curl -X "POST" -H "Content-Type: application/json" http://localhost:8080/invoke/01DJ900WFKNG8G00GZJ0000002
395398
>```
396399
397400
The result is once again the same.
@@ -405,7 +408,7 @@ function back.
405408
406409
![user input](images/userinput.png)
407410
>```sh
408-
> curl -H "Content-Type: application/json" -d '{"name":"Bob"}' http://localhost:8080/t/nodeapp/nodefn
411+
> curl -X "POST" -H "Content-Type: application/json" -d '{"name":"Bob"}' http://localhost:8080/invoke/01DJ900WFKNG8G00GZJ0000002
409412
>```
410413
411414
The result is once again the same.

0 commit comments

Comments
 (0)