-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
40 lines (31 loc) · 1.1 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
/* This is the code for the server. */
const express = require('express');
const puppeteer = require('puppeteer');
const IS_PRODUCTION = process.env.NODE_ENV === 'production';
const app = express();
const getBrowser = () => IS_PRODUCTION ?
// Connect to browserless so we don't run Chrome on the same hardware in production
puppeteer.connect({
browserWSEndpoint: 'wss://chrome.browserless.io?token=2ed5472c-a1e8-4f7e-b71e-8a6daa8e5898'
}) :
// Run the browser locally while in development
puppeteer.launch();
app.get('/image', async (req, res) => {
let browser = null;
try {
browser = await getBrowser();
const page = await browser.newPage();
await page.goto('https://bgoonzblog20-backup.netlify.app/');
const screenshot = await page.screenshot();
res.end(screenshot, 'binary');
} catch (error) {
if (!res.headersSent) {
res.status(400).send(error.message);
}
} finally {
if (browser) {
browser.close();
}
}
});
app.listen(8080, () => console.log('Listening on PORT: 8080'));