「Cloud Functions」の版間の差分
ナビゲーションに移動
検索に移動
| 46行目: | 46行目: | ||
#Hostingに対してカスタムドメインを設定 | #Hostingに対してカスタムドメインを設定 | ||
#firebase.json の hosting の rewrite に functions を追加 | #firebase.json の hosting の rewrite に functions を追加 | ||
| + | |||
| + | *Functions | ||
| + | <pre> | ||
| + | import * as functions from 'firebase-functions'; | ||
| + | import * as express from 'express'; | ||
| + | |||
| + | const app: express.Express = express(); | ||
| + | |||
| + | router.get('/api/test', (req, res) => { | ||
| + | res.send('TEST!'); | ||
| + | }); | ||
| + | |||
| + | export const apiService = functions.https.onRequest(app); | ||
| + | </pre> | ||
| + | *firebase.json | ||
<pre> | <pre> | ||
"hosting": [ | "hosting": [ | ||
| 51行目: | 66行目: | ||
: 省略 | : 省略 | ||
"rewrites": [ | "rewrites": [ | ||
| − | + | { | |
| − | "source": "/api", | + | "source": "/api/**", |
| − | "function": " | + | "function": "apiService" |
}, | }, | ||
{ | { | ||
| 59行目: | 74行目: | ||
"destination": "/index.html" | "destination": "/index.html" | ||
} | } | ||
| − | + | ] | |
} | } | ||
] | ] | ||
</pre> | </pre> | ||
2020年11月2日 (月) 15:12時点における版
| Google Cloud Platform | Firebase |
Cloud Functions
- Cloud Functions for Firebase
- Firebase CLI プロジェクトの Google Cloud Functions へファイルをアップロードして、Google Cloud Storageへ保存するコードをTypeScriptで書く
- Cloud Function関連ブログ
- Firebase関連ブログ
ローカル実行
- Firebase エミュレータを呼び出す
**package.json で定義された、firebase emulators:start を実行
$ npm install -g firebase-tools $ cd functions $ npm run serve
Expressと統合
$ npm install --save express $ npm install --save-dev @types/express
- index.ts
import * as functions from 'firebase-functions';
import * as express from 'express';
const app: express.Express = express();
const router: express.Router = express.Router()
app.use(router);
router.get('/test', (req, res) => {
res.send('TEST!');
});
export const api = functions.https.onRequest(app);
カスタムドメイン
- https://blog.katsubemakito.net/firebase/cloudfunctions-restfulapi-domain-rewrite
- https://firebase.google.com/docs/hosting/full-config?hl=ja
- https://firebase.google.com/docs/hosting/full-config?hl=ja#rewrites
- globパターンマッチング
- Hostingと併用することで対応
- Hostingに対してカスタムドメインを設定
- firebase.json の hosting の rewrite に functions を追加
- Functions
import * as functions from 'firebase-functions';
import * as express from 'express';
const app: express.Express = express();
router.get('/api/test', (req, res) => {
res.send('TEST!');
});
export const apiService = functions.https.onRequest(app);
- firebase.json
"hosting": [
{
: 省略
"rewrites": [
{
"source": "/api/**",
"function": "apiService"
},
{
"source": "**",
"destination": "/index.html"
}
]
}
]
© 2006 矢木浩人