以下是一个使用Playwright和Express框架创建简单Web应用程序的示例代码。该应用程序在前台显示一个页面,该页面使用Playwright在浏览器中自动化执行一些操作,并将结果返回给页面。

const express = require('express');
const { chromium } = require('playwright');

const app = express();

app.get('/', async (req, res) => {
  const browser = await chromium.launch();
  const page = await browser.newPage();

  await page.goto('https://www.example.com');
  const title = await page.title();
  const screenshot = await page.screenshot({ fullPage: true });

  await browser.close();

  res.send(`
    <h1>${title}</h1>
    <img src="data:image/png;base64,${screenshot.toString('base64')}" />
  `);
});

app.listen(3000, () => {
  console.log('App listening on port 3000!');
});