kintone のアプリデータをazure app service で公開その2です。
kintone への接続情報を環境変数で管理してみます。
概要
azure app service で kintone アプリデータ公開その1の続きです。
kintone 接続用の情報を環境変数に設定して、利用してみます。
環境変数の参考情報
npm dotenv を使うとよさそうです。
環境変数の準備
- dotenv をインストール
console.log
$ npm install dotenv --save
- kintone への接続情報を .env ファイルにまとめておきます。
# kintone connection
kbrowser_domainName=xxxxxxxxx.cybozu.com
kbrowser_basicUserName=
kbrowser_basicPassword=
kbrowser_userName=xxxxxxxxx
kbrowser_userPassword=xxxxxxxxx
環境変数の利用方法
app.js に require('dotenv').config(); を追加すると、他の js でも process.env.kbrowser_????? が利用できるようになります。
- app.js の変更
app.js
varcreateError=require('http-errors');varexpress=require('express');varpath=require('path');varcookieParser=require('cookie-parser');varlogger=require('morgan');require('dotenv').config();...
- kintoneConnections.js 追加
環境変数のkintone接続情報で、kintoneConnection を設定します。
kintoneConnections.js
constkintone=require('@kintone/kintone-js-sdk');constkintoneAuth=newkintone.Auth();if(process.env.kbrowser_basicUserName){constbasicUserName=process.env.kbrowser_basicUserNameconstbasicPassword=process.env.kbrowser_basicPasswordkintoneAuth.setBasicAuth({username:basicUserName,password:basicPassword});}constusername=process.env.kbrowser_userName;constpassword=process.env.kbrowser_userPassword;kintoneAuth.setPasswordAuth({username:username,password:password});constdomainName=process.env.kbrowser_domainName;constkintoneConnection=newkintone.Connection({domain:domainName,auth:kintoneAuth});module.exports=kintoneConnection;
- customer.js で kintoneConnection.js を利用するように変更します。
まだ、アプリの appID がリテラルです。
アプリ数が増えると変数管理が面倒なので、これも環境変数に持っていくかどうか検討要です。
customer.js
varexpress=require('express');varrouter=express.Router();constkintone=require('@kintone/kintone-js-sdk');constkintoneConnection=require('./kintoneConnection');/* GET customers listing. */router.get('/',function(req,res,next){constkintoneRecord=newkintone.Record({connection:kintoneConnection});constfinfos=[{label:'会社名',fcode:'会社名'},{label:'担当者名',fcode:'担当者名'},{label:'メールアドレス',fcode:'メールアドレス'}];constfields=finfos.map((finfo)=>{returnfinfo.fcode;});constappId=549;// target appIDconstparm={app:appId,query:'order by $id',fields:fields};kintoneRecord.getRecords(parm).then((rsp)=>{console.log('rsp',rsp);res.render('customer',{title:'顧客一覧',finfos:finfos,records:rsp.records});}).catch((err)=>{// The promise function always reject with KintoneAPIExeptionconsole.log(err);// res.render('forum', { title: 'rex0220 forum', user: 'error' });});});module.exports=router;
azure app service の環境変数
app service の構成で、環境変数を設定できます。
vscode によるazure app service の環境変数の管理
Application Setting で、azure app service の環境変数をダウンロード・アップロードできます。
ローカルPCの環境変数ファイル「.env」と別に、azure の環境変数ファイル「.azure_prod_env」などで管理できます。
vscode の Application Setting で、右クリックするとダウンロード・アップロードのメニューが表示されます。
あとがき
kintone 接続情報を環境変数で管理出来ましたが、アプリIDやAPITokenの情報も環境変数で管理すると対象のアプリ数が増えると管理・運用が大変そうです。
個別のアプリ情報は、できれば kintone アプリで管理したいと思いますが、kintone の API 回数制限などが課題です。