{"id":11553,"date":"2017-08-01T10:30:54","date_gmt":"2017-08-01T01:30:54","guid":{"rendered":"http:\/\/sitest.jp\/blog\/?p=11553"},"modified":"2017-08-01T10:23:27","modified_gmt":"2017-08-01T01:23:27","slug":"5%e5%88%86%e3%81%a7%e3%82%b5%e3%83%bc%e3%83%90%e3%83%ac%e3%82%b9%e3%82%92%e4%bd%93%e9%a8%93%ef%bc%81-cloud-functions-for-firebase-%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e3%81%bf%e3%82%88%e3%81%86","status":"publish","type":"post","link":"https:\/\/sitest.jp\/blog\/?p=11553","title":{"rendered":"5\u5206\u3067\u30b5\u30fc\u30d0\u30ec\u30b9\u3092\u4f53\u9a13\uff01 Cloud Functions for Firebase \u3092\u4f7f\u3063\u3066\u307f\u3088\u3046"},"content":{"rendered":"<p>\u8fd1\u65e5\u3001\u30b5\u30fc\u30d0\u30ec\u30b9\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u3068\u3044\u3046\u8a00\u8449\u3092\u8033\u306b\u3059\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u306d\u3002<br \/>\n\u30b5\u30fc\u30d0\u30ec\u30b9\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u3068\u306f\u3001\u30b5\u30fc\u30d0\u3092\u610f\u8b58\u305b\u305a\u3068\u3082\u30a2\u30d7\u30ea\u3092\u4f5c\u6210\u3067\u304d\u308b\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u306e\u3053\u3068\u3067\u3042\u308a\u3001\u30b5\u30fc\u30d0\u30fc\u30ec\u30b9\u30b3\u30f3\u30d4\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u30b5\u30fc\u30d3\u30b9\u306b\u3088\u308a\u306a\u308a\u305f\u3063\u3066\u3044\u308b\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u3002<\/p>\n<p>\u6709\u540d\u3069\u3053\u308d\u3060\u3068 <a href=\"http:\/\/docs.aws.amazon.com\/ja_jp\/lambda\/latest\/dg\/welcome.html\" target=\"_blank\" rel=\"nofollow\">AWS Lambda<\/a> \u3084 <a href=\"https:\/\/cloud.google.com\/functions\/?hl=ja\" target=\"_blank\" rel=\"nofollow\">Google Cloud Functions<\/a> \u306a\u3069\u304c\u3042\u3052\u3089\u308c\u307e\u3059\u306d\u3002<\/p>\n<p>Faas( Function as a Service ) \u3068\u3082\u547c\u3070\u308c\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001\u5c11\u3057\u4f3c\u305f\u3082\u306e\u3067\u3001 Baas( Backend as a service )\u3068\u3044\u3046\u63d0\u4f9b\u5f62\u614b\u3082\u5b58\u5728\u3057\u307e\u3059\u3002<br \/>\n<a href=\"https:\/\/firebase.google.com\/\" target=\"_blank\" rel=\"nofollow\">Firebase<\/a> \u304c\u6709\u540d\u3067\u3001\u30d0\u30c3\u30af\u30a8\u30f3\u30c9\u3092\u307b\u307c\u63d0\u4f9b\u3057\u3066\u304f\u308c\u308b\u306e\u3067\u3001\u624b\u8efd\u306a\u53cd\u9762\u3001\u81ea\u7531\u5ea6\u304c\u4f4e\u3044\u3068\u3044\u3046\u8ab2\u984c\u304c\u3042\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u306e\u8ab2\u984c\u3092\u89e3\u6c7a\u3059\u308b\u306e\u304c\u3001\u4eca\u56de\u3054\u7d39\u4ecb\u3059\u308b\u300c <a href=\"https:\/\/firebase.google.com\/docs\/functions\/\" target=\"_blank\" rel=\"nofollow\">Cloud Functions for Firebase<\/a> \u300d\u3067\u3059\u3002<br \/>\nFirebase \u3092\u4f7f\u3044\u305f\u3044\u3051\u3069\u3001\u3082\u3046\u5c11\u3057\u81ea\u7531\u5ea6\u3082\u6b32\u3057\u3044\u3068\u3044\u3063\u305f\u4eba\u5411\u3051\u306e\u30b5\u30fc\u30d3\u30b9\u3067\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f Cloud Functions for Firebase \u306e Functions \u3092\u4f7f\u3046\u3068\u3053\u308d\u307e\u3067\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><!--more--><\/p>\n<div class=\"frame_headding\">\n<h3 style=\"text-align: left;\">Cloud Functions for Firebase \u3063\u3066\u306a\u306b\uff1f<\/h3>\n<\/div>\n<p><img loading=\"lazy\" src=\"https:\/\/storage.googleapis.com\/sitest-wp\/blog\/wp-content\/uploads\/2017\/07\/cfff_ss1-1024x579.png\" alt=\"Cloud Functions for Firebase\" width=\"604\" height=\"342\" class=\"alignnone size-large wp-image-11559\" \/><\/p>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3078\u306e\u66f8\u304d\u8fbc\u307f\u3084HTTPS\u30ea\u30af\u30a8\u30b9\u30c8\u306a\u3069\u306e\u30a4\u30d9\u30f3\u30c8\u3092\u30c8\u30ea\u30ac\u30fc\u306b\u3057\u3066\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3067\u304d\u308b\u6a5f\u80fd\u3067\u3059\u3002<br \/>\n\u30b5\u30dd\u30fc\u30c8\u8a00\u8a9e\u3068\u3057\u3066\u306f\u3001 Node.js \u306e\u307f\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u30b5\u30f3\u30d7\u30eb\u304c\u3044\u304f\u3064\u304b\u7528\u610f\u3055\u308c\u3066\u304a\u308a\u307e\u3059\u306e\u3067\u3001\u4e0b\u8a18\u306e GitHub \u304b\u3089\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n<p><a href=\"https:\/\/github.com\/firebase\/functions-samples\" target=\"_blank\" rel=\"nofollow\">https:\/\/github.com\/firebase\/functions-samples<\/a><\/p>\n<p>\u203b \u73fe\u5728\u3001 Cloud Functions for Firebase \u306f Beta \u7248\u3068\u306a\u3063\u3066\u304a\u308a\u307e\u3059\u3002<\/p>\n<div class=\"frame_headding\">\n<h3 style=\"text-align: left;\">Cloud Functions for Firebase \u3092\u4f7f\u3063\u3066\u307f\u3088\u3046<\/h3>\n<\/div>\n<p>Node.js, npm \u3068\u3082\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u304a\u308a\u3001 Firebase \u306e\u30a2\u30ab\u30a6\u30f3\u30c8\u3092\u6301\u3063\u3066\u3044\u308b\u3053\u3068\u3092\u524d\u63d0\u3068\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>Firebase \u306e\u6982\u8981\u304b\u3089\u30a2\u30ab\u30a6\u30f3\u30c8\u4f5c\u6210\u307e\u3067\u306f\u4e0b\u8a18\u306e\u8a18\u4e8b\u3092\u53c2\u8003\u306b\u4f5c\u6210\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><a href=\"http:\/\/sitest.jp\/blog\/?p=11311\" target=\"_blank\">Firebase \u3092\u4f7f\u3063\u3066\u30b5\u30a4\u30c8\u3092\u7121\u6599\u3067\u516c\u958b\u3057\u3066\u307f\u308b<\/a><\/p>\n<p>\u305d\u308c\u3067\u306f\u3001\u307e\u305a Firebase \u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3057\u307e\u3057\u3087\u3046\u3002<br \/>\n\u4eca\u56de\u306f\u3001\u300c cloud-functions \u300d\u3068\u3044\u3046\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u540d\u3067\u4f5c\u6210\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" src=\"https:\/\/storage.googleapis.com\/sitest-wp\/blog\/wp-content\/uploads\/2017\/07\/cfff_ss2-1024x490.png\" alt=\"Firebase \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u4f5c\u6210\" width=\"604\" height=\"289\" class=\"alignnone size-large wp-image-11560\" \/><\/p>\n<p>\u5916\u90e8\u3068\u901a\u4fe1\u3092\u884c\u3046\u305f\u3081\u3001\u30d7\u30e9\u30f3\u3092 Spark \u304b\u3089 Blaze \u306b\u5909\u66f4\u3057\u3066\u304a\u3044\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><img loading=\"lazy\" src=\"https:\/\/storage.googleapis.com\/sitest-wp\/blog\/wp-content\/uploads\/2017\/07\/cfff_ss3-1024x503.png\" alt=\"Firebase \u30d7\u30e9\u30f3\u5909\u66f4\" width=\"604\" height=\"297\" class=\"alignnone size-large wp-image-11561\" \/><\/p>\n<p>\u6b21\u306f CUI \u4e0a\u3067\u4f5c\u696d\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u304a\u597d\u304d\u306a\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u308a\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code>mkdir cloud_functions\ncd cloud_functions\n<\/code><\/pre>\n<p>\u6b21\u306bFirebase \u306b\u30ed\u30b0\u30a4\u30f3\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code>firebase login\n<\/code><\/pre>\n<p>Functions \u3092\u8ffd\u52a0\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<pre><code>firebase init functions\n<\/code><\/pre>\n<p>Select a default Firebase project for this directory \u3068\u3067\u307e\u3059\u306e\u3067\u3001<br \/>\ncloud-functions \u3092\u9078\u629e\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><img loading=\"lazy\" src=\"https:\/\/storage.googleapis.com\/sitest-wp\/blog\/wp-content\/uploads\/2017\/07\/cfff_ss4.png\" alt=\"firebase init functions\" width=\"856\" height=\"455\" class=\"alignnone size-full wp-image-11562\" \/><\/p>\n<p>\u3059\u308b\u3068\u3001 functions \u3068\u3044\u3046\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u304c\u4f5c\u6210\u3055\u308c\u307e\u3059\u306e\u3067\u3001<br \/>\n\u79fb\u52d5\u3057\u3066\u3001 index.js \u304c\u5b58\u5728\u3059\u308b\u306e\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n<p>index.js \u3092\u4e0b\u8a18\u306e\u3088\u3046\u306b\u66f8\u304d\u63db\u3048\u3066\u4e0b\u3055\u3044\u3002<\/p>\n<pre><code>\/\/ The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.\nconst functions = require('firebase-functions');\n\n\/\/ The Firebase Admin SDK to access the Firebase Realtime Database. \nconst admin = require('firebase-admin');\nadmin.initializeApp(functions.config().firebase);\n\n\/\/ Take the text parameter passed to this HTTP endpoint and insert it into the\n\/\/ Realtime Database under the path \/messages\/:pushId\/original\nexports.addMessage = functions.https.onRequest((req, res) =&gt; {\n  \/\/ Grab the text parameter.\n  const original = req.query.text;\n  \/\/ Push the new message into the Realtime Database using the Firebase Admin SDK.\n  admin.database().ref('\/messages').push({original: original}).then(snapshot =&gt; {\n    \/\/ Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.\n    res.redirect(303, snapshot.ref);\n  });\n});\n\n\/\/ Listens for new messages added to \/messages\/:pushId\/original and creates an\n\/\/ uppercase version of the message to \/messages\/:pushId\/uppercase\nexports.makeUppercase = functions.database.ref('\/messages\/{pushId}\/original')\n    .onWrite(event =&gt; {\n      \/\/ Grab the current value of what was written to the Realtime Database.\n      const original = event.data.val();\n      console.log('Uppercasing', event.params.pushId, original);\n      const uppercase = original.toUpperCase();\n      \/\/ You must return a Promise when performing asynchronous tasks inside a Functions such as\n      \/\/ writing to the Firebase Realtime Database.\n      \/\/ Setting an \"uppercase\" sibling in the Realtime Database returns a Promise.\n      return event.data.ref.parent.child('uppercase').set(uppercase);\n    });\n<\/code><\/pre>\n<p>\u3053\u3053\u3067\u306f\u3001 addMessage() \u3068\u3044\u3046\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u66f8\u304d\u8fbc\u3080\u305f\u3081\u306eURL\u3092\u5410\u304d\u51fa\u3057\u3066\u304f\u308c\u308b\u95a2\u6570\u3068<br \/>\nmakeUppercase() \u3068\u3044\u3046\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u66f8\u304d\u8fbc\u307f\u3092\u30c8\u30ea\u30ac\u30fc\u3057\u3066\u3001\u30c6\u30ad\u30b9\u30c8\u3092\u5927\u6587\u5b57\u306b\u5909\u63db\u3059\u308b\u95a2\u6570\u3092\u4f7f\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306b\u30c7\u30d7\u30ed\u30a4\u3057\u3066\u3001\u51e6\u7406\u3092\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code>firebase deploy --only functions\n<\/code><\/pre>\n<p>\u30c7\u30d7\u30ed\u30a4\u304c\u5b8c\u4e86\u3059\u308b\u3068\u3001URL\u304c\u5410\u304d\u51fa\u3055\u308c\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" src=\"https:\/\/storage.googleapis.com\/sitest-wp\/blog\/wp-content\/uploads\/2017\/07\/cfff_ss5.png\" alt=\"addMessage() URL\u5410\u304d\u51fa\u3057\" width=\"752\" height=\"94\" class=\"alignnone size-full wp-image-11566\" \/><\/p>\n<p>{URL} + ?text=uppercasemetoo<br \/>\n\u3092\u30d6\u30e9\u30a6\u30b6\u3067\u53e9\u3044\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u305d\u306e\u5f8c\u3001 Firebase \u306e\u7ba1\u7406\u753b\u9762\u306b\u623b\u308a\u3001 Database \u3092\u958b\u3044\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><img loading=\"lazy\" src=\"https:\/\/storage.googleapis.com\/sitest-wp\/blog\/wp-content\/uploads\/2017\/07\/cfff_ss6-1024x542.png\" alt=\"Frebase database \u78ba\u8a8d\" width=\"604\" height=\"320\" class=\"alignnone size-large wp-image-11580\" \/><\/p>\n<p>original: &#8220;uppercasemetoo&#8221; \u3068<br \/>\n\u5927\u6587\u5b57\u306b\u306a\u3063\u305f uppercase: &#8220;UPPERCASEMETOO&#8221; \u304c\u4fdd\u5b58\u3055\u308c\u3066\u3044\u308b\u306e\u304c\u308f\u304b\u308a\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001Functions \u306e\u30c0\u30c3\u30b7\u30e5\u30dc\u30fc\u30c9\u3092\u78ba\u8a8d\u3057\u3066\u3082\u3001\u6307\u5b9a\u3057\u305f\u95a2\u6570\u304c\u53e9\u304b\u308c\u3066\u3044\u308b\u306e\u3092\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" src=\"https:\/\/storage.googleapis.com\/sitest-wp\/blog\/wp-content\/uploads\/2017\/07\/cfff_ss7-1024x539.png\" alt=\"Firebase Functions \u78ba\u8a8d\" width=\"604\" height=\"318\" class=\"alignnone size-large wp-image-11569\" \/><\/p>\n<p>\u5c11\u3057\u3067\u3059\u304c\u3001\u30b5\u30fc\u30d0\u30ec\u30b9\u3092\u4f53\u9a13\u3067\u304d\u307e\u3057\u305f\u306d\u3002<\/p>\n<p>\u3044\u304b\u304c\u3067\u3057\u305f\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n<p>\u4eca\u56de\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3078\u306e\u4fdd\u5b58\u304b\u3089\u5927\u6587\u5b57\u5909\u63db\u307e\u3067\u884c\u3044\u307e\u3057\u305f\u304c\u3001\u4ed6\u306b\u3082\u3001\u8a8d\u8a3c\u30a4\u30d9\u30f3\u30c8\u3001 \u5206\u6790\u30a4\u30d9\u30f3\u30c8\u3068\u69d8\u3005\u306a\u30b5\u30f3\u30d7\u30eb\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u307e\u3059\u306e\u3067\u3001\u662f\u975e\u3001\u8a66\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd1\u65e5\u3001\u30b5\u30fc\u30d0\u30ec\u30b9\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u3068\u3044\u3046\u8a00\u8449\u3092\u8033\u306b\u3059\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u306d\u3002 \u30b5\u30fc\u30d0\u30ec\u30b9\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u3068\u306f\u3001\u30b5\u30fc\u30d0\u3092\u610f\u8b58\u305b\u305a\u3068\u3082\u30a2\u30d7\u30ea\u3092\u4f5c\u6210\u3067\u304d\u308b\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u306e\u3053\u3068\u3067\u3042\u308a\u3001\u30b5\u30fc\u30d0\u30fc\u30ec\u30b9\u30b3\u30f3\u30d4\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u30b5\u30fc\u30d3\u30b9\u306b\u3088\u308a\u306a\u308a\u305f\u3063\u3066\u3044\u308b\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u3002 \u6709\u540d\u3069\u3053\u308d\u3060\u3068 AWS Lambda \u3084 Google Cloud Functions \u306a\u3069\u304c\u3042\u3052\u3089\u308c\u307e\u3059\u306d\u3002 Faas( Function as a Se [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[51,37,27],"tags":[],"acf":false,"_links":{"self":[{"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/posts\/11553"}],"collection":[{"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/comments?post=11553"}],"version-history":[{"count":12,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/posts\/11553\/revisions"}],"predecessor-version":[{"id":11575,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/posts\/11553\/revisions\/11575"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/media\/11559"}],"wp:attachment":[{"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/media?parent=11553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/categories?post=11553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/tags?post=11553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}