This repository has been archived by the owner on Jun 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (44 loc) · 1.61 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
var express = require('express');
const PORT = process.env.PORT;
var debug = require('debug');
var bodyParser = require("body-parser");
const puppeteer = require('puppeteer');
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/', function(req, res){
res.send("G5 Page Renderer");
});
app.post('/rendered_page', function(req, res){
var pageURL=req.body.url;
async function getPage() {
// configure Puppeteer
let browser = await puppeteer.launch({ headless: true,
args: ['--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--single-process'] });
let page = await browser.newPage();
// Let's speed this thing up by not loading any images
await page.setRequestInterception(true);
page.on('request', request => {
if (request.resourceType() === 'image')
request.abort();
else
request.continue();
});
// Visit Page URL
await page.goto(pageURL)
// Wait for network to go idle, or up to 5 seconds, whichever comes first
await Promise.race([
page.waitForNavigation({waitUntil: 'networkidle0'}),
page.waitFor(5000)
]);
// Grab the <html> element, close the browser, call it good.
let theDOM = await page.evaluate(() => document.documentElement.outerHTML);
await browser.close();
return theDOM
}
getPage().then((body)=>{res.send(body);});
});
app.listen(PORT);