fast72 commited on
Commit
a343199
·
verified ·
1 Parent(s): 6953c95

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +26 -5
server.js CHANGED
@@ -1,27 +1,48 @@
1
  const express = require("express");
2
  const { chromium } = require("playwright");
 
3
 
4
  const app = express();
 
5
  app.use(express.raw({ type: "text/html", limit: "1mb" }));
6
 
 
 
 
 
 
 
7
  app.post("/v1/start", async (req, res) => {
8
  try {
9
  const html = req.body.toString();
10
  if (!html.trim()) return res.sendStatus(400);
11
 
12
- const browser = await chromium.launch({ args: ["--no-sandbox"] });
 
 
 
 
 
 
 
 
 
 
13
  const page = await browser.newPage();
 
14
  await page.setContent(html, { waitUntil: "load" });
15
-
16
- const buffer = await page.screenshot({ type: "png" });
17
 
18
  await browser.close();
19
 
20
- res.setHeader("Content-Type", "image/png");
21
- res.send(buffer);
22
  } catch (err) {
23
  console.error(err);
24
  res.sendStatus(500);
 
 
25
  }
26
  });
27
 
 
1
  const express = require("express");
2
  const { chromium } = require("playwright");
3
+ const os = require("os");
4
 
5
  const app = express();
6
+ const tmp = os.tmpdir();
7
  app.use(express.raw({ type: "text/html", limit: "1mb" }));
8
 
9
+ app.use('/t', express.static(tmp));
10
+
11
+ const utils = {
12
+ randomName: (str = '') => Math.random().toString(36).slice(2) + str,
13
+ }
14
+
15
  app.post("/v1/start", async (req, res) => {
16
  try {
17
  const html = req.body.toString();
18
  if (!html.trim()) return res.sendStatus(400);
19
 
20
+ const browser = await chromium.launch({
21
+ args: [
22
+ '--incognito',
23
+ '--single-process',
24
+ '--no-sandbox',
25
+ '--no-zygote',
26
+ '--no-cache'
27
+ ],
28
+ executablePath: process.env.CHROME_BIN,
29
+ headless: true
30
+ });
31
  const page = await browser.newPage();
32
+ await page.goto('https://example.com');
33
  await page.setContent(html, { waitUntil: "load" });
34
+ const output = `${tmp}/${utils.randomName('.jpg')}`
35
+ const buffer = await page.screenshot({ path: output });
36
 
37
  await browser.close();
38
 
39
+ res.json({ result: output })
40
+
41
  } catch (err) {
42
  console.error(err);
43
  res.sendStatus(500);
44
+ } finally {
45
+ if (browser) await browser.close()
46
  }
47
  });
48