Skip to content

Commit 0b3146f

Browse files
coco-supertanhe123
authored andcommitted
use http trigger demo instead of api gateway for getting_stated document (#242)
1 parent 1440408 commit 0b3146f

2 files changed

Lines changed: 112 additions & 71 deletions

File tree

docs/usage/getting_started-zh.md

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,33 @@
1212

1313
## 示例
1414

15-
下面我们用一个简单的 helloworld 示例演示 fun 如何使用。首先在项目根目录下创建一个 helloworld.js 文件。
15+
下面我们用一个简单的 http 触发器示例演示 fun 如何使用。首先在项目根目录下创建一个 index.js 文件。
1616

1717
```javascript
18-
exports.handler = function(event, context, callback) {
19-
var response = {
20-
isBase64Encoded: false,
21-
statusCode: 200,
22-
body: 'hellow wrold'
23-
};
24-
callback(null, response);
18+
var getRawBody = require('raw-body')
19+
20+
module.exports.initializer = function(context, callback) {
21+
console.log("initializer invoked");
22+
callback(null, '');
23+
}
24+
25+
module.exports.handler = function (request, response, context) {
26+
// get request body
27+
getRawBody(request, function (err, body) {
28+
var respBody = {
29+
headers: request.headers,
30+
url: request.url,
31+
path: request.path,
32+
queries: request.queries,
33+
method: request.method,
34+
clientIP: request.clientIP,
35+
body: body.toString()
36+
};
37+
38+
response.setStatusCode(200);
39+
response.setHeader('content-type', 'application/json');
40+
response.send(JSON.stringify(respBody, null, 4));
41+
});
2542
};
2643
```
2744

@@ -31,46 +48,50 @@ exports.handler = function(event, context, callback) {
3148
ROSTemplateFormatVersion: '2015-09-01'
3249
Transform: 'Aliyun::Serverless-2018-04-03'
3350
Resources:
34-
fc: # service name
51+
local-http-test:
3552
Type: 'Aliyun::Serverless::Service'
3653
Properties:
37-
Description: 'fc test'
38-
helloworld: # function name
54+
Description: 'local invoke demo'
55+
nodejs8:
3956
Type: 'Aliyun::Serverless::Function'
4057
Properties:
41-
Handler: helloworld.handler
58+
Handler: index.handler
59+
CodeUri: nodejs8/
60+
Description: 'http trigger demo with nodejs8!'
4261
Runtime: nodejs8
43-
CodeUri: './'
44-
Timeout: 60
45-
46-
HelloworldGroup: # Api Group
47-
Type: 'Aliyun::Serverless::Api'
48-
Properties:
49-
StageName: RELEASE
50-
DefinitionBody:
51-
'/': # request path
52-
get: # http method
53-
x-aliyun-apigateway-api-name: hello_get # api name
54-
x-aliyun-apigateway-fc:
55-
arn: acs:fc:::services/${fc.Arn}/functions/${helloworld.Arn}/
62+
Initializer: index.initializer
63+
Events:
64+
http-test:
65+
Type: HTTP
66+
Properties:
67+
AuthType: ANONYMOUS
68+
Methods: ['GET', 'POST', 'PUT']
5669
```
5770
5871
代码以及模板文件编写完成后,就可以使用 `fun deploy` 命令一键将服务部署到线上环境了:
5972

6073
```shell
6174
$ fun deploy
62-
63-
Waiting for service fc to be deployed...
64-
service fc deploy success
65-
Waiting for api gateway HelloworldGroup to be deployed...
66-
URL: GET http://2c2c4629c42f45a1b73000dd2a8b34b2-cn-shanghai.alicloudapi.com/
67-
stage: RELEASE, deployed, version: 20180627110526681
68-
stage: PRE, undeployed
69-
stage: TEST, undeployed
70-
api gateway HelloworldGroup deploy success
75+
using region: cn-shanghai
76+
using accountId: ***********8320
77+
using accessKeyId: ***********1EXB
78+
using timeout: 10
79+
80+
Waiting for service local-http-test to be deployed...
81+
Waiting for function nodejs8 to be deployed...
82+
Waiting for packaging function nodejs8 code...
83+
package function nodejs8 code done
84+
Waiting for HTTP trigger http-test to be deployed...
85+
methods: GET
86+
url: https://1984152879328320.cn-shanghai.fc.aliyuncs.com/2016-08-15/proxy/local-http-test/nodejs8/
87+
function http-test deploy success
88+
function nodejs8 deploy success
89+
service local-http-test deploy success
7190
```
7291

73-
打开浏览器访问 `http://2c2c4629c42f45a1b73000dd2a8b34b2-cn-shanghai.alicloudapi.com/` 这个地址即可预览效果。
92+
打开浏览器访问 `https://1984152879328320.cn-shanghai.fc.aliyuncs.com/2016-08-15/proxy/local-http-test/nodejs8/` 即可触发函数的执行。对于 HTTP 触发器,服务端会为 response header 中强制添加 `content-disposition: attachment` 字段,此字段会使得返回结果在浏览器中以附件的方式打开。此字段无法覆盖,使用自定义域名将不受影响。更多信息请参见 [函数计算常见问题](https://help.aliyun.com/knowledge_detail/56103.html?spm=a2c4g.11186623.6.711.117c28acEBZTtF#HTTP-Trigger-compulsory-header)。
93+
94+
如果想在本地单步调试、运行 http trigger 的函数,可以参考 [开发函数计算的正确姿势 —— Http Trigger 本地运行调试](https://yq.aliyun.com/articles/683683)。
7495

7596
## 配置
7697

docs/usage/getting_started.md

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,33 @@ Now you are ready to use the fun command.
1212

1313
### Example
1414

15-
Here is an example. First, create a `helloworld.js` file in the project root directory:
15+
Here is an example. First, create a `index.js` file in the project root directory:
1616

1717
```javascript
18-
exports.handler = function(event, context, callback) {
19-
var response = {
20-
isBase64Encoded: false,
21-
statusCode: 200,
22-
body: 'hello world'
23-
};
24-
callback(null, response);
18+
var getRawBody = require('raw-body')
19+
20+
module.exports.initializer = function(context, callback) {
21+
console.log("initializer invoked");
22+
callback(null, '');
23+
}
24+
25+
module.exports.handler = function (request, response, context) {
26+
// get request body
27+
getRawBody(request, function (err, body) {
28+
var respBody = {
29+
headers: request.headers,
30+
url: request.url,
31+
path: request.path,
32+
queries: request.queries,
33+
method: request.method,
34+
clientIP: request.clientIP,
35+
body: body.toString()
36+
};
37+
38+
response.setStatusCode(200);
39+
response.setHeader('content-type', 'application/json');
40+
response.send(JSON.stringify(respBody, null, 4));
41+
});
2542
};
2643
```
2744

@@ -31,46 +48,49 @@ Then, let's configure related services. Create a `template.yml` file in the proj
3148
ROSTemplateFormatVersion: '2015-09-01'
3249
Transform: 'Aliyun::Serverless-2018-04-03'
3350
Resources:
34-
fc: # service name
51+
local-http-test:
3552
Type: 'Aliyun::Serverless::Service'
3653
Properties:
37-
Description: 'fc test'
38-
helloworld: # function name
54+
Description: 'local invoke demo'
55+
nodejs8:
3956
Type: 'Aliyun::Serverless::Function'
4057
Properties:
41-
Handler: helloworld.handler
58+
Handler: index.handler
59+
CodeUri: nodejs8/
60+
Description: 'http trigger demo with nodejs8!'
4261
Runtime: nodejs8
43-
CodeUri: './'
44-
Timeout: 60
45-
46-
HelloworldGroup: # Api Group
47-
Type: 'Aliyun::Serverless::Api'
48-
Properties:
49-
StageName: RELEASE
50-
DefinitionBody:
51-
'/': # request path
52-
get: # http method
53-
x-aliyun-apigateway-api-name: hello_get # api name
54-
x-aliyun-apigateway-fc:
55-
arn: acs:fc:::services/${fc.Arn}/functions/${helloworld.Arn}/
62+
Initializer: index.initializer
63+
Events:
64+
http-test:
65+
Type: HTTP
66+
Properties:
67+
AuthType: ANONYMOUS
68+
Methods: ['GET', 'POST', 'PUT']
5669
```
5770
5871
After the template file and code are written, you can use the deploy command to deploy the service, function and api gateway online.
5972
6073
```shell
61-
$fun deploy
62-
63-
Waiting for service fc to be deployed...
64-
service fc deploy success
65-
Waiting for api gateway HelloworldGroup to be deployed...
66-
URL: GET http://2c2c4629c42f45a1b73000dd2a8b34b2-cn-shanghai.alicloudapi.com/
67-
stage: RELEASE, deployed, version: 20180627110526681
68-
stage: PRE, undeployed
69-
stage: TEST, undeployed
70-
api gateway HelloworldGroup deploy success
74+
$ fun deploy
75+
using region: cn-shanghai
76+
using accountId: ***********8320
77+
using accessKeyId: ***********1EXB
78+
using timeout: 10
79+
80+
Waiting for service local-http-test to be deployed...
81+
Waiting for function nodejs8 to be deployed...
82+
Waiting for packaging function nodejs8 code...
83+
package function nodejs8 code done
84+
Waiting for HTTP trigger http-test to be deployed...
85+
methods: GET
86+
url: https://1984152879328320.cn-shanghai.fc.aliyuncs.com/2016-08-15/proxy/local-http-test/nodejs8/
87+
function http-test deploy success
88+
function nodejs8 deploy success
89+
service local-http-test deploy success
7190
```
91+
Open the browser and visit `https://1984152879328320.cn-shanghai.fc.aliyuncs.com/2016-08-15/proxy/local-http-test/nodejs8/` to trigger the execution of the function. For HTTP triggers, server will add a `content-disposition: attachment` header in the response headers by force, which will cause the browser to download response content as an attachment. This header cannot be overwritten, and using custom domain can avoid this header being added. For more information, please refer to [FAQ](https://help.aliyun.com/knowledge_detail/56103.html?spm=a2c4g.11186623.6.711.117c28acEBZTtF#HTTP-Trigger-compulsory-header).
7292

73-
Open the browser to access `http://2c2c4629c42f45a1b73000dd2a8b34b2-cn-shanghai.alicloudapi.com/` to view the result.
93+
If you want to debug and run HTTP trigger functions locally, you can refer to [开发函数计算的正确姿势 —— Http Trigger 本地运行调试](https://yq.aliyun.com/articles/683683).
7494

7595
## Configuration
7696

0 commit comments

Comments
 (0)