-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
48 lines (41 loc) · 1.49 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
const express = require('express');
const path = require('path');
const puppeteer = require('puppeteer');
const execFile = require('child_process').execFile;
const fs = require('fs');
const PORT = process.env.PORT || 3000;
express()
.use(express.static(path.join(__dirname, 'public')))
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
.get('/', async (req, res) => {
const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
const page = await browser.newPage();
await page.setViewport({ width: 600, height: 800 });
await page.goto(process.env.SCREENSHOT_URL || 'https://darksky.net/details/40.7127,-74.0059/2021-1-6/us12/en');
await page.screenshot({
path: '/tmp/screenshot.png',
});
await browser.close();
await convert('/tmp/screenshot.png');
screenshot = fs.readFileSync('/tmp/screenshot.png');
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': screenshot.length,
});
return res.end(screenshot);
})
.listen(PORT, () => console.log(`Listening on ${PORT}`));
function convert(filename) {
return new Promise((resolve, reject) => {
const args = [filename, '-gravity', 'center', '-extent', '600x800', '-colorspace', 'gray', '-depth', '8', filename];
execFile('convert', args, (error, stdout, stderr) => {
if (error) {
console.error({ error, stdout, stderr });
reject();
} else {
resolve();
}
});
});
}