mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
more tutorial
This commit is contained in:
3
examples/tutorial/hello/node/.gitignore
vendored
Normal file
3
examples/tutorial/hello/node/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
node_modules/
|
||||
func.yaml
|
||||
Dockerfile
|
||||
41
examples/tutorial/hello/node/README.md
Normal file
41
examples/tutorial/hello/node/README.md
Normal file
@@ -0,0 +1,41 @@
|
||||
## Quick Example for a NodeJS Function (4 minutes)
|
||||
|
||||
This example will show you how to test and deploy a Node function to Oracle Functions.
|
||||
|
||||
```sh
|
||||
# create your func.yaml file
|
||||
fn init <YOUR_DOCKERHUB_USERNAME>/hello
|
||||
# build the function
|
||||
fn build
|
||||
# test it
|
||||
cat hello.payload.json | fn run
|
||||
# push it to Docker Hub
|
||||
fn push
|
||||
# Create a route to this function on Oracle Functions
|
||||
fn routes create myapp /hello
|
||||
```
|
||||
|
||||
Now surf to: http://localhost:8080/r/myapp/hello
|
||||
|
||||
## Dependencies
|
||||
|
||||
Create a [package.json](https://docs.npmjs.com/getting-started/using-a-package.json) file in your functions directory.
|
||||
|
||||
Run:
|
||||
|
||||
```sh
|
||||
docker run --rm -v "$PWD":/function -w /function funcy/node:dev npm install
|
||||
```
|
||||
|
||||
Then everything should work.
|
||||
|
||||
For example, using the `package.json` file in this directory which includes the [request](https://www.npmjs.com/package/request) package, you can add this to func.js and it will work:
|
||||
|
||||
```js
|
||||
var request = require('request');
|
||||
request('http://www.google.com', function (error, response, body) {
|
||||
if (!error && response.statusCode == 200) {
|
||||
console.log(body) // Show the HTML for the Google homepage.
|
||||
}
|
||||
})
|
||||
```
|
||||
9
examples/tutorial/hello/node/func.js
Normal file
9
examples/tutorial/hello/node/func.js
Normal file
@@ -0,0 +1,9 @@
|
||||
name = "World";
|
||||
fs = require('fs');
|
||||
try {
|
||||
obj = JSON.parse(fs.readFileSync('/dev/stdin').toString())
|
||||
if (obj.name != "") {
|
||||
name = obj.name
|
||||
}
|
||||
} catch(e) {}
|
||||
console.log("Hello", name, "from Node!");
|
||||
3
examples/tutorial/hello/node/hello.payload.json
Normal file
3
examples/tutorial/hello/node/hello.payload.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"name": "Johnny"
|
||||
}
|
||||
7
examples/tutorial/hello/node/package.json
Normal file
7
examples/tutorial/hello/node/package.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "my-awesome-func",
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"request": "^2.78.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user