-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (48 loc) · 1.45 KB
/
index.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
const puppeteer = require('puppeteer');
const uuid = require('uuid');
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const path = require('path');
var browser;
async function init() {
browser = await puppeteer.launch();
}
var port = 8083;
init();
app.use(bodyParser.json());
app.use('/tmp', express.static(path.join(__dirname, 'tmp')))
app.post('/screenshot', async (req, res) => {
try {
url = req.body.url;
var page = await browser.newPage();
var id = uuid.v4();
var response = await page.goto(url);
statusCode = response.status();
if (statusCode == 200) {
const container = await page.$('#app');
const boundingBox = await container.boundingBox();
await page.screenshot({
path: `./tmp/${id}.png`,
clip: {
x: boundingBox.x,
y: boundingBox.y,
width: 2126,
height: boundingBox.height
}
});
res.end(`http://localhost:${port}/tmp/${id}.png`);
}
if (statusCode == 403) {
res.status(403).end('Forbidden');
}
if (statusCode == 500) {
res.status(500).end('Internal Server Error');
}
} catch (error) {
console.log(error)
res.status(500).end('Puppteer internal error');
}
});
app.listen(port)
console.log("server start");