NodeJS ã§RESTãµã¼ããæçã§ï¼Salesforceã®ã³ã¼ã«ã¢ã¦ããã¹ããµã¼ãï¼
Salesforce ã§ã»ã¼æ¨æºã®éçºç°å¢ sfdx
ã§ãããã³ã¬ãnodejs
çµç±ã§ã¤ã³ã¹ãã¼ã«åºæ¥ãã®ã§ãããã
éã«è¨ãã¨ãnodejs
ã¨è¦ªåæ§ãé«ãã¨ãè¨ãã¾ãã
ããã§ãSalesforce ã®ã³ã¼ã«ã¢ã¦ãï¼Apex
*1 ãããSalesforce å¤ã® HTTP ãªã¯ã¨ã¹ããéä¿¡ããä»çµã¿ï¼ã®ãã¹ãç¨ãµã¼ããããã£ã¨ä½ããã¨ããã®ãä»åã®èª²é¡ã
ç®æ¬¡
NodeJS ã§ç§æ®ºRESTãµã¼ã
å ¨é¨ããã©ã«ãã§OKãªã®ã§ã³ãã³ããå®è¡
npm init
: nodejs ã®ããã¸ã§ã¯ããä½ænpm install -d express
: Nodejs ã®è»½éãµã¼ãã©ã¤ãã©ãªã¤ã³ã¹ãã¼ã«npm install -d compression
: ã¬ã¹ãã³ã¹ã®å§ç¸®ã©ã¤ãã©ãª
index.js
ãä½æãã¦
const compression = require('compression'); const express = require('express'); const app = express(); const port = 3000; app.use(compression()); app.use(express.json()); app.get('/test', (req, res) => { res.json({ message: 'Welcome to JSON!' }); }) app.use('/', express.static('static')); app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`); });
è¦ãã¾ã¾ã§ããã/test
ã«ã¢ã¯ã»ã¹ãã㨠JSON å¿çãè¿ãããã以å¤ã®URLã¢ã¯ã»ã¹ã¯ static
ãã©ã«ãå
ãåç
§ãã¾ãã
node index.js
ã§èµ·åã
> node index.js Example app listening at http://localhost:3000
ã¡ãªã¿ã«ãExpressJS ã®å ¬å¼ãµã¤ãã¯ä¸è¨ã§ãã
ngrok 㧠URL ãåå¾ãã
ä¸è¨ã¾ã§ã ã¨ããã¼ã«ã«ãµã¼ãï¼ã¨ããããèªåã®PCå
ãµã¼ãï¼ã«ã¨ã©ã¾ã£ã¦ãã¾ã£ã¦ãSalesforce ããã¢ã¯ã»ã¹ã§ãã¾ããã
ã§ãå
¬éã®çºã«ãã¡ãã¡ãµã¼ãå¥ç´ãã¦ã»ããã¢ãããã¦â¦ãªãã¦ãã£ã¦ããã¾ããããã
ã¨ãããã¨ã§ãngrok
ã使ãã¾ãã
ç¡æã§ä½¿ããã®ã§ç»é²ãã¾ããã
ãã°ã¤ã³ããã°ãããªæãã®ç»é¢ã«ãªãã¾ãã
ãã¦ã³ãã¼ãï¼ã»ããã¢ããã¯ç»é¢è¡¨ç¤ºãããæé ãå®è¡ãã¾ãã
ngrok config add-authtoken XXXXXXXXXXXXXXXXXXXXXXXX
ãå®è¡ããã¿ã¤ãã³ã°ã§èªè¨¼ãã§ãã¾ãã
ãã¨ã¯
> ngrok http 3000
試ãã«ã¢ã¯ã»ã¹ãã¦ã¿ãã¨
å¾ã¯ Salesforce å´ãã㯠URL ãæå®ã§ã¢ã¯ã»ã¹ããã°æ¤è¨¼ã«å©ç¨ã§ãã¾ãã
è£è¶³
ngrok ã®æéä½ç³»ãçºãã¦ã¿ãã¨ãå®ã¯ãµã¼ãã¹å
¬éã§ä½¿ãã®ãå
¨ç¶ã¢ãªã§ã¯ãªããã¨æãã
ã¨ããã®ãããã¼ããåºå®ã§ãããããµã¼ãå´ããå¶å¾¡åºæ¥ãã
ã¬ãã¬ãã«åºããã¤ã³ãã©å
ããããµã¼ãã¹ã»ã¢ããªã ããå
¬éããã¨ããããªãã®é²å¾¡ãæ§ç¯åºæ¥ãããã
ãã ã©ã¤ã»ã³ã¹è¦ãã°è§£ãããç¬èªãã¡ã¤ã³ã ã£ãããURL ãªã¶ã¼ãããã¼ããã©ã³ã·ã³ã°çã¯å ¨ã¦ææãªã®ã§ãããã¾ã§ã課éããã°ãã ãã©ãã
*1:Salesforce å°ç¨ã®éçºè¨èª