LoginSignup
0
0

ディレクトリ構成

├── Dockerfile
├── docker-compose.yml
├── index.js
├── node_modules
├── package.json
└── yarn.lock

Dockerfile

nodeのバージョンを20にしているのは以下のガイドから。LTSがv20なのと、v22はプレビュー版のみ、v18は非推奨が結構近いので、v20にしている。

スクリーンショット 2024-07-04 0.14.12.png

FROM node:20.11.0-bullseye-slim

RUN apt update && apt install

WORKDIR /app

COPY package.json yarn.lock /app/

RUN yarn

COPY . /app/

CMD ["yarn", "start"]

package.json

import を使いたいので、"type": "module"を指定。

{
  "main": "index.js",
  "type": "module",
  "license": "ISC",
  "scripts": {
    "start": "npx functions-framework --target=myHttpFunction"
  },
  "dependencies": {
    "@google-cloud/functions-framework": "^3.0.0"
  }
}

index.js

公式はCommonJSを使ってインポートしているが、ESModulesを使ってインポートする。正直好みの問題。Node.jsはCommonJSで書かれていること多いよね。

import ff from '@google-cloud/functions-framework';

ff.http('myHttpFunction', (req, res) => {
  res.send('OK');
});

起動

プロジェクトrootで実施する。

docker compose up -d
curl http://localhost:8080/ // -> OK

これで完成。

おわりに

この記事をベースとして、オレオレCloud Functions環境を作っていこうと思います。
TypeScirptを使ったCloud Functionsの環境構築方法を後日紹介しようと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0