Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require("express");
|
| 2 |
+
const puppeteer = require("puppeteer");
|
| 3 |
+
const bodyParser = require("body-parser");
|
| 4 |
+
const cors = require("cors");
|
| 5 |
+
const hljs = require("highlight.js");
|
| 6 |
+
|
| 7 |
+
const app = express();
|
| 8 |
+
app.use(cors());
|
| 9 |
+
app.use(bodyParser.json());
|
| 10 |
+
|
| 11 |
+
app.post("/generate", async (req, res) => {
|
| 12 |
+
const { code, language } = req.body;
|
| 13 |
+
if (!code) return res.status(400).json({ error: "Code is required" });
|
| 14 |
+
|
| 15 |
+
const highlightedCode = language
|
| 16 |
+
? hljs.highlight(code, { language }).value
|
| 17 |
+
: hljs.highlightAuto(code).value;
|
| 18 |
+
|
| 19 |
+
const htmlContent = `
|
| 20 |
+
<html>
|
| 21 |
+
<head>
|
| 22 |
+
<link href="https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;600&display=swap" rel="stylesheet">
|
| 23 |
+
<style>
|
| 24 |
+
* { margin: 0; padding: 0; box-sizing: border-box; }
|
| 25 |
+
body { background: #282c34; display: flex; align-items: center; justify-content: center; height: 100vh; padding: 20px; }
|
| 26 |
+
.window { background: #1e1e1e; border-radius: 12px; min-width: 500px; padding-top: 40px; box-shadow: 0px 10px 30px rgba(0, 0, 0, 0.4); border: 1px solid rgba(255, 255, 255, 0.1); }
|
| 27 |
+
.title-bar { display: flex; align-items: center; height: 35px; padding-left: 15px; position: absolute; top: 0; left: 0; width: 100%; background: rgba(255, 255, 255, 0.05); border-top-left-radius: 12px; border-top-right-radius: 12px; }
|
| 28 |
+
.dot { width: 14px; height: 14px; border-radius: 50%; margin-right: 8px; }
|
| 29 |
+
.red { background: #ff5f56; } .yellow { background: #ffbd2e; } .green { background: #27c93f; }
|
| 30 |
+
pre { padding: 20px; font-family: 'Fira Code', monospace; font-size: 16px; white-space: pre-wrap; background: #1e1e1e; border-bottom-left-radius: 12px; border-bottom-right-radius: 12px; }
|
| 31 |
+
code { display: block; color: #ffffff; font-size: 16px; }
|
| 32 |
+
</style>
|
| 33 |
+
</head>
|
| 34 |
+
<body>
|
| 35 |
+
<div class="window">
|
| 36 |
+
<div class="title-bar">
|
| 37 |
+
<div class="dot red"></div>
|
| 38 |
+
<div class="dot yellow"></div>
|
| 39 |
+
<div class="dot green"></div>
|
| 40 |
+
</div>
|
| 41 |
+
<pre><code>${highlightedCode}</code></pre>
|
| 42 |
+
</div>
|
| 43 |
+
</body>
|
| 44 |
+
</html>
|
| 45 |
+
`;
|
| 46 |
+
|
| 47 |
+
const browser = await puppeteer.launch({
|
| 48 |
+
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH,
|
| 49 |
+
args: ["--no-sandbox", "--disable-setuid-sandbox"],
|
| 50 |
+
});
|
| 51 |
+
|
| 52 |
+
const page = await browser.newPage();
|
| 53 |
+
await page.setContent(htmlContent, { waitUntil: "networkidle0" });
|
| 54 |
+
const screenshot = await page.screenshot({ type: "png" });
|
| 55 |
+
await browser.close();
|
| 56 |
+
|
| 57 |
+
res.setHeader("Content-Type", "image/png");
|
| 58 |
+
res.send(screenshot);
|
| 59 |
+
});
|
| 60 |
+
|
| 61 |
+
app.listen(7860);
|