-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·76 lines (66 loc) · 1.97 KB
/
app.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
const path = require('path');
const express = require('express');
const puppeteer = require('puppeteer');
const app = express();
const port = 5000;
const puppeteerLaunchOptions = {
args: ['--no-sandbox'],
};
const captureScreenshot = async (url, viewportWidth, viewportHeight) => {
try {
const browser = await puppeteer.launch(puppeteerLaunchOptions);
const page = await browser.newPage();
await page.setViewport({ width: viewportWidth, height: viewportHeight });
await page.goto(url, {
waitUntil: 'networkidle2',
timeout: 0,
});
const image = await page.screenshot({
path: 'screenshot_full.jpg',
fullPage: true,
delay: '500ms',
});
await browser.close();
return image;
} catch (error) {
console.error(error);
throw error;
}
};
app.get('/', async (req, res) => {
const url = req.query.url;
if (!url) {
res.status(400).send('Missing URL query parameter.');
return;
}
try {
const image = await captureScreenshot(url, 1440, 1024);
res.contentType('image/png');
res.send(image);
} catch (error) {
res.status(500).send('Error capturing screenshot.');
}
});
app.get('/soil', async (req, res) => {
const url = 'https://nodered.denoodev.com/ui/#!/1?socketid=fmH2JUlerO7KcBXzAAEU';
try {
const image = await captureScreenshot(url, 1440, 1400);
res.contentType('image/png');
res.send(image);
} catch (error) {
res.status(500).send('Error capturing screenshot.');
}
});
app.get('/weather', async (req, res) => {
const url = 'https://nodered.denoodev.com/ui/#!/0?socketid=fmH2JUlerO7KcBXzAAEU';
try {
const image = await captureScreenshot(url, 1440, 1024);
res.contentType('image/png');
res.send(image);
} catch (error) {
res.status(500).send('Error capturing screenshot.');
}
});
const server = app.listen(port, () => {
console.log(`Your app is listening on port ${port}`);
});