iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 6
1
Microsoft Azure

白眼狼的30天Azure跳槽計畫系列 第 6

Day [6] Azure Functions-本機執行&測試

本機執行 Functions

https://ithelp.ithome.com.tw/upload/images/20200921/20108281Jr4Dho6HgH.jpg
我們打開lineWebhook function資料夾中的index.ts檔案
可以看到下列範例code 為一個簡單的 HTTP function:

import { AzureFunction, Context, HttpRequest } from "@azure/functions"

const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
    context.log('HTTP trigger function processed a request.');
    const name = (req.query.name || (req.body && req.body.name));
    const responseMessage = name
        ? "Hello, " + name + ". This HTTP triggered function executed successfully."
        : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";

    context.res = {
        status: 200,
        body: responseMessage
    };
};

export default httpTrigger;

在終端機中輸入npm start 可以在本機執行該functions專案
執行前記得先
npm install
安裝npm moudule

$ npm install
$ npm start

執行後可看見以下畫面代表執行成功 function 已在本機執行,預設執行在port:7071
https://ithelp.ithome.com.tw/upload/images/20200921/20108281fmeZtARE4y.jpg

測試 Functions

測試function 可以透過 Postman API測試工具
https://ithelp.ithome.com.tw/upload/images/20200921/20108281rknFX86red.jpg

打開Postman後只需將AP(function)網址輸入,選擇Http method 輸入變數後即可輕鬆測試
https://ithelp.ithome.com.tw/upload/images/20200921/20108281uXpDqszmqT.jpg


上一篇
Day [5] Azure Functions - 使用CLI 建構本地專案
下一篇
Day [7] Azure Functions-將函式部署至Azure
系列文
白眼狼的30天Azure跳槽計畫30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言