-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
39 lines (31 loc) · 939 Bytes
/
server.js
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
const http = require("node:http");
const fs = require("node:fs");
const puppeteer = require("puppeteer");
const server = http.createServer(async (req, res) => {
try {
const browser = await puppeteer.launch({
headless: "new",
});
const page = await browser.newPage();
await page.setContent(
fs.readFileSync("./grid.html", "utf8").replace(
"__DATA__",
JSON.stringify([
{ make: "Toyota", model: "Banana", price: 35000 },
{ make: "Ford", model: "Hotdog", price: 32000 },
{ make: "Porsche", model: "Taco", price: 72000 },
]),
),
);
const buffer = await page.pdf();
res.writeHead(200, { "Content-Type": "application/pdf" });
res.end(buffer);
} catch (error) {
console.log(error);
res.writeHead(500);
res.end("Internal Server Error");
}
});
server.listen(3000, () => {
console.log("server listening on 3000");
});