-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
43 lines (33 loc) · 1.23 KB
/
Copy pathserver.mjs
File metadata and controls
43 lines (33 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import express from "express"
import puppeteer from "puppeteer"
const app = express()
app.get("/screenshot", async (req, res) => {
let browser
try {
// Launch a Chromium browser instance
browser = await puppeteer.launch({
headless: true, // running with headless mode
args: ["--no-sandbox", "--disable-setuid-sandbox"], // minimal arguments required to start headless browser
})
// Create a new page in the browser
const page = await browser.newPage()
// Navigate to the specified URL
await page.goto(req.query.url)
// Take a screenshot of the current page you can set type 'png' | 'jpeg' | 'webp'
const buffer = await page.screenshot({ type: "png" })
// Set the content type for the response set correct header based on type you set above
res.setHeader("Content-Type", "image/png")
// Send the screenshot as a response
res.send(Buffer.from(buffer))
} catch (error) {
console.error("Error taking screenshot:", error)
res.status(500).send("Internal Server Error")
} finally {
// Close the browser instance
await browser.close()
}
})
const PORT = process.env.PORT || 3000
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`)
})