{"id":970,"date":"2020-11-10T00:05:36","date_gmt":"2020-11-09T15:05:36","guid":{"rendered":"https:\/\/www.typea.info\/blog\/?p=970"},"modified":"2020-11-10T00:08:14","modified_gmt":"2020-11-09T15:08:14","slug":"google_cloud_functions_express_from_angular","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2020\/11\/10\/google_cloud_functions_express_from_angular\/","title":{"rendered":"Google Cloud Functions  \u3092 Express \u3067\u5b9f\u88c5\u3057 Firebase Hosting \u306e Angular \u304b\u3089\u547c\u3073\u51fa\u3059"},"content":{"rendered":"<p>\u00a0<\/p>\n<p><a href=\"https:\/\/www.amazon.co.jp\/GCP%E3%81%AE%E6%95%99%E7%A7%91%E6%9B%B8II-%E3%80%90%E3%82%B3%E3%83%B3%E3%83%86%E3%83%8A%E9%96%8B%E7%99%BA%E7%B7%A8%E3%80%91-%E3%82%AF%E3%83%A9%E3%82%A6%E3%83%89%E3%82%A8%E3%83%BC%E3%82%B9%E6%A0%AA%E5%BC%8F%E4%BC%9A%E7%A4%BE-ebook\/dp\/B088LZGPM5\/ref=as_li_ss_il?adgrpid=69256641020&amp;dchild=1&amp;gclid=Cj0KCQiA7qP9BRCLARIsABDaZzhSa9NMvMe-iqz0XENNZX_aYobpm2zH0HS-JWSuOMHICWzzkkmifcoaAhaZEALw_wcB&amp;hvadid=341020075177&amp;hvdev=c&amp;hvlocphy=1009461&amp;hvnetw=g&amp;hvqmt=b&amp;hvrand=14755255238009501448&amp;hvtargid=kwd-697048140206&amp;hydadcr=15817_11177327&amp;jp-ad-ap=0&amp;keywords=gcp%E3%81%AE%E6%95%99%E7%A7%91%E6%9B%B8&amp;qid=1604933319&amp;sr=8-1&amp;linkCode=li2&amp;tag=typea09-22&amp;linkId=0d251dfbf6a56d64245360800b1c6080&amp;language=ja_JP\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" src=\"\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=B088LZGPM5&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22&amp;language=ja_JP\" border=\"0\" \/><\/a><img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"https:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;language=ja_JP&amp;l=li2&amp;o=9&amp;a=B088LZGPM5\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/><\/p>\n<p>\u00a0<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2020\/10\/30\/google_cloud_functions_express\/\">Cloud Functions \u306e\u4e2d\u8eab\u3092 Express \u306b\u7f6e\u304d\u63db\u3048<\/a>\u3001<a href=\"https:\/\/www.typea.info\/blog\/index.php\/2020\/11\/03\/google_cloud_functions_cusgtom_domain\/\">\u30ab\u30b9\u30bf\u30e0\u30c9\u30e1\u30a4\u30f3\u3092\u8a2d\u5b9a\u3057\u305f\u3002<\/a><\/p>\n<p>\u4ee5\u524d\u884c\u3063\u305f\u3001<a href=\"https:\/\/www.typea.info\/blog\/index.php\/2020\/08\/11\/angular-cloud-functions\/\">Firebase Hosting \u4e0a\u306e Angular\u304b\u3089\u547c\u3073\u51fa\u3057\u540c\u69d8\u3001<\/a>\u758e\u901a\u3092\u78ba\u8a8d\u3059\u308b\u3068\u3053\u308d\u307e\u3067\u3001\u3044\u304f\u3064\u304b\u30cf\u30de\u308a\u30dd\u30a4\u30f3\u30c8\u304c\u3042\u3063\u305f\u306e\u3067\u3001\u7559\u610f\u70b9\u3092\u30e1\u30e2\u3002<\/p>\n<h3>1.Functions\u5074\u306e\u5b9f\u88c5<\/h3>\n<p>\u4ee5\u4e0b\u7559\u610f\u70b9<\/p>\n<ul>\n<li><a href=\"https:\/\/firebase.google.com\/docs\/functions\/callable-reference\">https.onCall<\/a> \u306e\u4ed5\u69d8\u3068\u3057\u3066\u3001HTTP POST\u30e1\u30bd\u30c3\u30c9\u3067\u3046\u3051\u308b\u5fc5\u8981\u304c\u3042\u308b<\/li>\n<li>\u30ed\u30fc\u30ab\u30eb\u304b\u3089\u30c6\u30b9\u30c8\u547c\u3073\u51fa\u3057\u3059\u308b\u3068\u3001<a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/HTTP\/CORS\">CORS<\/a>\u306b\u306b\u3072\u3063\u304b\u304b\u308b\u306e\u3067\u3001Access-Control-Allow\uff65\uff65\uff65\u3000\u30d8\u30c3\u30c0\u3092\u6307\u5b9a\u3059\u308b<\/li>\n<li>\u5fdc\u7b54\u306eJSON\u306e\u7d50\u679c\u3092data \u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u6307\u5b9a\u3059\u308b<\/li>\n<\/ul>\n<p>\u00a0<\/p>\n<pre class=\"prettyprint\">import * as functions from 'firebase-functions';\nimport * as express from 'express';\n\nconst app: express.Express = express();\napp.use((req, res, next) =&gt; {\n    res.header(\"Access-Control-Allow-Origin\", \"*\");\n    res.header(\"Access-Control-Allow-Headers\", \"Origin, X-Requested-With, Content-Type, Accept\");\n    next();\n});\n\nconst router: express.Router = express.Router()\napp.use(router);\nrouter.post('\/api\/test', (req, res) =&gt; {\n    res.json({'data':{'result':'ok'}});\n});\nexport const apiService = functions.https.onRequest(app);\n<\/pre>\n<p>\u00a0<\/p>\n<h3>2.\u547c\u3073\u51fa\u3057Angular\u5074\u306e\u5b9f\u88c5<\/h3>\n<ul>\n<li>httpsCallable \u306b \u30d1\u30b9\u3092\u6307\u5b9a\u3067\u304d\u308b<\/li>\n<\/ul>\n<pre class=\"prettyprint\">import { Injectable } from '@angular\/core';\nimport { Observable, from } from 'rxjs';\nimport { AngularFireFunctions } from '@angular\/fire\/functions';\n\n@Injectable({\n  providedIn: 'root'\n})\nexport class HogeService {\n  apiFunc : any;\n  constructor(\n    private functions: AngularFireFunctions\n  ) { \n    this.apiFunc = this.functions.httpsCallable('apiService\/api\/test');\n  }\n\n  async calc() {\n    const obs = this.apiFunc() as Observable;\n    let res = await obs.toPromise();\n    console.log(res);\n  }\n}\n<\/pre>\n<h3>3.\u5b9f\u884c\u7d50\u679c<\/h3>\n<h4>3.1 Firebase \u30a8\u30df\u30e5\u30ec\u30fc\u30bf\u306b\u3088\u308b\u52d5\u4f5c\u78ba\u8a8d<\/h4>\n<p>$ firebase emulators:start \u3067\u5b9f\u884c\u3001Visual Studio Code\u306e URL\u3092 Command+\u30af\u30ea\u30c3\u30af\u3067\u30b3\u30f3\u30bd\u30fc\u30eb\u8d77\u52d5<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"firebase_emulator_start.png\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/11\/firebase_emulator_start.png\" alt=\"EMU START\" width=\"554\" height=\"214\" border=\"0\" \/><\/p>\n<p>\u30a8\u30df\u30e5\u30ec\u30fc\u30bf\u30fc\u30b3\u30f3\u30bd\u30fc\u30eb\u753b\u9762\u304c\u8d77\u52d5\u3059\u308b\u306e\u3067\u3001Hosting\u30a8\u30df\u30e5\u30ec\u30fc\u30bf\u30fc\u304b\u3089\u30da\u30fc\u30b8\u3092\u958b\u304d\u3001\u4e0a\u8a18\u5b9f\u88c5\u3057\u305f\u30da\u30fc\u30b8\u304b\u3089 Functions\u3092\u547c\u3073\u51fa\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"firebase_emurator_ok.png\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/11\/firebase_emurator_ok.png\" alt=\"Emulator OK\" width=\"598\" height=\"302\" border=\"0\" \/><\/p>\n<p>\u60f3\u5b9a\u3057\u305f\u7d50\u679c\u304c\u53d6\u5f97\u3067\u304d\u305fOK\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" title=\"function_result_json.png\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/11\/function_result_json.png\" alt=\"RESULT JSON\" width=\"280\" height=\"146\" border=\"0\" \/><\/p>\n<p>\u3061\u306a\u307f\u306b\u3001firebase\u306b\u30c7\u30d7\u30ed\u30a4\u3057\u3001ng serve \u3067\u5358\u72ec\u3067angular \u3092\u30ed\u30fc\u30ab\u30eb\u3067\u5b9f\u884c\u3057\u3066\u3082\u540c\u69d8\u306e\u7d50\u679c\u3092\u5f97\u308b\u3053\u3068\u304c\u3067\u304d\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0 \u00a0 Cloud Functions \u306e\u4e2d\u8eab\u3092 Express \u306b\u7f6e\u304d\u63db\u3048\u3001\u30ab\u30b9\u30bf\u30e0\u30c9\u30e1\u30a4\u30f3\u3092\u8a2d\u5b9a\u3057\u305f\u3002 \u4ee5\u524d\u884c\u3063\u305f\u3001Firebase Hosting \u4e0a\u306e Angular\u304b\u3089\u547c\u3073\u51fa\u3057\u540c\u69d8\u3001\u758e\u901a\u3092\u78ba\u8a8d\u3059\u308b\u3068\u3053\u308d\u307e\u3067\u3001\u3044 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":969,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[102,118,104,112],"tags":[],"class_list":["post-970","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular","category-express","category-firebase","category-google-cloud-functions"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=970"}],"version-history":[{"count":2,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/970\/revisions"}],"predecessor-version":[{"id":972,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/970\/revisions\/972"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media\/969"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}