fast72 commited on
Commit
52a1756
·
verified ·
1 Parent(s): 85d8828

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +16 -11
server.js CHANGED
@@ -15,20 +15,25 @@ function randomFilename() {
15
  }
16
 
17
  app.post("/v1/start", async (req, res) => {
18
- const html = req.body;
19
- if (!html) return res.status(400).send("HTML required");
 
20
 
21
- const browser = await chromium.launch({ args: ["--no-sandbox"] });
22
- const page = await browser.newPage();
23
- await page.setContent(html);
24
 
25
- const filename = randomFilename();
26
- const filepath = path.join(tmpDir, filename);
27
- await page.screenshot({ path: filepath });
28
 
29
- await browser.close();
30
 
31
- res.json({ url: `${req.protocol}://${req.get("host")}/t/${filename}` });
 
 
 
 
32
  });
33
 
34
  app.use("/t", (req, res, next) => {
@@ -48,4 +53,4 @@ app.use("/t", (req, res, next) => {
48
  next();
49
  }, express.static(tmpDir));
50
 
51
- app.listen(process.env.PORT || 7860);
 
15
  }
16
 
17
  app.post("/v1/start", async (req, res) => {
18
+ try {
19
+ const html = req.body;
20
+ if (!html) return res.status(400).json({ error: "HTML required" });
21
 
22
+ const browser = await chromium.launch({ args: ["--no-sandbox", "--disable-setuid-sandbox"] });
23
+ const page = await browser.newPage();
24
+ await page.setContent(html, { waitUntil: "load" });
25
 
26
+ const filename = randomFilename();
27
+ const filepath = path.join(tmpDir, filename);
28
+ await page.screenshot({ path: filepath });
29
 
30
+ await browser.close();
31
 
32
+ res.json({ url: `${req.protocol}://${req.get("host")}/t/${filename}` });
33
+ } catch (error) {
34
+ console.error("Error:", error);
35
+ res.status(500).json({ error: "Internal Server Error" });
36
+ }
37
  });
38
 
39
  app.use("/t", (req, res, next) => {
 
53
  next();
54
  }, express.static(tmpDir));
55
 
56
+ app.listen(process.env.PORT || 7860, () => console.log("Server running..."));