LoginSignup
9
13

More than 5 years have passed since last update.

Azure Functionsメモ

Last updated at Posted at 2018-05-29

想定読者

  • Kuduがわかる人
  • Azure Functionsでちょっと不便を感じてる人

Problem and Solution

P1. 関数作るたびにnpm installするの面倒くさい

S1. wwwrootでnpm init, npm installして各Functionからシンボリックリンクを貼る

  • 実行コマンド(Kuduで実行):
ln -s D:\home\site\wwwroot\node_modules D:\home\site\wwwroot\HttpTriggerJS1




P2. Functions間の共通関数を作って使いたい

S2. Kuduでwwwrootに移動 => ~/wwwrootに共通関数を定義 => 各Functionでインポート

  • 共通関数を定義
~/wwwroot/common.js
module.exports.testFunction = function (x){
  ~
  ~
  ~
}
  • 各Functionでインポート
~/wwwroot/HttpTriggerJS1/index.js
const common = require('../common.js');
 ~
 ~
 ~
common.testFunction(input);
 ~




P3. ルーティングしたい

S3. function.jsonにプロパティ"route: ~"を追加

  • パスパラメータ名定義
function.json
{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "route": "{path}/{subpath}"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ],
  "disabled": false
}

* 上記のように定義すると、https://xxxxxxxxxx.azurewebsites.net/api/HttpTriggerJS1/{path}/{subpath} がエンドポイントになる。

  • パスパラメータ取得とルーティング
~/wwwroot/HttpTriggerJS1/index.js
//パスパラメータ設定
const PATH = context.bindingData.path;
const SUBPATH = context.bindingData.subpath;

//以下にルーティングロジックを実装(ifやswitch等)




P4. Function内で自分の関数名を取得したい

S4. context.executionContext.functionNameで取得可

* カレントディレクトリは、context.executionContext.functionDirectoryで取得可。




P5. Function名を変更したい

S5. (変更はできないので)KuduでFunctionのフォルダを丸ごとコピーして別名で保存

cp -rf D:\home\site\wwwroot\HttpTriggerJS1 D:\home\site\wwwroot\HttpTriggerJS1_renamed




P6. 他のFunctionで算出された値を参照したい

S6. 参照先でグローバル変数に代入しておけば他のFunctionから参照可

* ただし参照先のFunctionsを実行してからでないと参照不可。




P7. 環境変数を設定して使いたい

S7. [アプリケーション設定]で設定し、process.env.propertyNameで参照可能。




P8. エディタ画面小さすぎて開発しづらい

S8. [プラットフォーム機能] => [開発ツール] => [App Serviceエディタ]でBotとかと同じ大きいエディタが使える。ただテストはできそうなところは見当たらず。




P9. 画面下の実行ログが出ない

S9. バグなので他の関数を選んでから戻ったり関数を再起動するしかない。

おわりに

 Kudu使えばそりゃだいたいできますよねといった感じですが、一つでも役に立つものがあれば嬉しいです。これからもっと効率よく開発できるように頑張ります。


9
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
13