Subversion Repository Public Repository

Nextrek

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const express = require('express');
const bodyParser = require('body-parser');
const http = require('http');
const path = require('path');
const app = express();
const puppeteer = require('puppeteer');

//lettura porta da secondo parametro come da richiesta
var port = process.argv [ 2 ];

if ( port && port.length > 0 ){
 port = parseInt ( port );
}else{
 port = 3000;
}

var page;
var browser;

async function readHtml(url){
	
	var toReturn = "";
	
	//if (!browser){
		browser = await puppeteer.launch();
	//}
	
	//if (!page){
		page = await browser.newPage();
	//}
	
	await page.setViewport({width:1920, height: 900}); //Custom dimensions
	//await page.goto('http://82.223.81.183:8084/', {timeout: 60000, waitUntil: 'networkidle0'});
	try{
		console.log("Reading page: " + url);
		
		await page.goto(url, {timeout: 90000, waitUntil: 'networkidle0'});

		console.log("page gone to: " + url);
		
		//console.log(await page.content());
		toReturn = await page.content();
		//await page.screenshot({path: 'screenshot.png', fullpage:true});

		console.log("closing browser");
		await browser.close();
	
		console.log("Page finished: " + url);
	
		return toReturn;
	}catch(e){
		toReturn = "timeout error - " + e;
		browser.close();
		browser = undefined;
		page = undefined;
		console.log("Error", e);
		return toReturn;
	}
}

app.use(express.json()); // to support JSON-encoded bodies

var jsonParser = bodyParser.json();

//endpoint
app.post('/', jsonParser, function(req, res) {
    
	var params = req.body;
	try{
		readHtml(params.url).then(function(a){
			//res.send(JSON.stringify(params.url) + " - " + a);
			res.send(a);
		});
	}catch(e){
		res.send("timeout error 2 " - e);
	}
	
});
//FINE endpoint

app.get('/', function(req, res) {
    
	var params = [];
	params.url = req.query.url;
	try{
		readHtml(params.url).then(function(a){
			//res.send(JSON.stringify(params.url) + " - " + a);
			res.send(a);
		});
	}catch(e){
		res.send("timeout error 2 " - e);
	}
	
});
//FINE endpoint




app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
    extended: true
}));

server = http.createServer(app).listen(port);
console.log("Server start... On port: :" + port);




//readHtml();

Commits for Nextrek/puppeteer/puppeteer.js

Diff revisions: vs.
Revision Author Commited Message
1114 Diff Diff MOliva picture MOliva Fri 14 Sep, 2018 16:26:00 +0000
1113 Diff Diff MOliva picture MOliva Wed 12 Sep, 2018 16:57:19 +0000
1112 Diff Diff MOliva picture MOliva Wed 12 Sep, 2018 16:55:13 +0000
1111 Diff Diff MOliva picture MOliva Wed 12 Sep, 2018 16:54:27 +0000
1110 Diff Diff MOliva picture MOliva Wed 12 Sep, 2018 16:47:48 +0000
1109 Diff Diff MOliva picture MOliva Wed 12 Sep, 2018 14:55:27 +0000
1108 Diff Diff MOliva picture MOliva Wed 12 Sep, 2018 14:50:36 +0000
1107 Diff Diff MOliva picture MOliva Wed 12 Sep, 2018 14:48:22 +0000
1105 MOliva picture MOliva Tue 11 Sep, 2018 16:26:14 +0000