0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【ES Modules】Cloud Functionsでrequire()ではなくimportを使う【index.mjs】

Last updated at Posted at 2023-03-16

type: moduleを使う場合

package.json
{
  "name": "sample-http",
  "type": "module",
  "version": "0.0.1"
}
index.js
/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
export const helloWorld = (req, res) => {
  let message = req.query.message || req.body.message || 'Hello World!'
  res.status(200).send(message)
}

index.mjsを使う場合

コンソールエディタでシンタックスハイライトがきかないので、あまりオススメはしません

package.json
{
  "name": "sample-http",
  "main": "index.mjs",
  "version": "0.0.1"
}
index.mjs
/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
export const helloWorld = (req, res) => {
  let message = req.query.message || req.body.message || 'Hello World!'
  res.status(200).send(message)
}

Puppeteerを使った例

Node.js 16を選択し、メモリを2GB以上にしましょう

package.json
{
  "name": "sample-http",
  "main": "index.mjs",
  "version": "0.0.1",
  "dependencies": {
    "puppeteer": "^19.7.5"
  }
}
index.mjs
import puppeteer from 'puppeteer'

/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
export const helloWorld = async (req, res) => {
  await puppeteer.createBrowserFetcher().download(puppeteer.defaultBrowserRevision)
  const browser = await puppeteer.launch()
  const page = await browser.newPage()
  await page.goto('https://example.com/')
  res.send(await page.content())
  await browser.close()
}
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?