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()
}