Update server.js
Browse files
server.js
CHANGED
|
@@ -15,20 +15,25 @@ function randomFilename() {
|
|
| 15 |
}
|
| 16 |
|
| 17 |
app.post("/v1/start", async (req, res) => {
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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..."));
|