forked from mailtarget/headless-chrome-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
77 lines (68 loc) · 1.83 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const express = require('express')
const app = express()
const bodyParser = require('body-parser');
const puppeteer = require('puppeteer')
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
let browser
(async () => {
browser = await puppeteer.launch({
headless: true,
slowMo: 250,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
})();
app.use(express.json());
app.get('/', (req, res) => {
res.send('headless chrome api is ready to serve')
})
app.post('/content', async (req, res) => {
const url = req.body.url
const js = req.body.js
try {
if (url) {
console.log('open ' + url)
const content = await ssr(url, js)
res.json(content)
} else {
res.status(400).send("missing input value")
}
} catch (error) {
console.error(error)
res.status(500).send(error)
}
})
app.post('/pdf', async (req, res) => {
const {html, paperSize} = req.body
try {
const page = await browser.newPage();
await page.setContent(html);
const result = await page.pdf({
format: paperSize || 'A4',
printBackground: true,
});
res.contentType('application/pdf');
res.send(result);
page.close()
} catch (error) {
res.status(500).send({
error: true,
msg: error.message,
})
}
})
async function ssr(url, js = false) {
const page = await browser.newPage();
if (js) {
await page.goto(url, {waitUntil: 'networkidle0'});
} else {
await page.goto(url);
}
const html = await page.content(); // serialized HTML of page DOM.
page.close()
return html;
}
const port = 3000
app.listen(port, () => {
console.log(`Example app listening on port ${port}!`)
})