-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
67 lines (55 loc) · 1.93 KB
/
server.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
/**
* Simple service for fetching screenshots
*/
var express = require('express');
var app = express();
// define the public directory for static assets
var public_dir = __dirname + '/public';
//setup
app.use(express.static(public_dir));
//fetch url
app.get('/thumb/:url', function (req, res) {
// get url to process
var url_to_process = req.params.url;
if (url_to_process === undefined || url_to_process == '') {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end("404 Not Found");
}
// phantomjs screenshot
var phantom = require('phantom');
var viewportSize;
if (req.query.width !== undefined && req.query.height !== undefined) {
viewportSize = {
width: req.query.width, height: req.query.height
};
} else {
viewportSize = {
width: 1280, height: 800
};
}
var clipRect = { left: 0, top: 0, width: 0, height: 0 };
if (req.query.frontOnly !== undefined) {
clipRect.width = viewportSize.width;
clipRect.height = viewportSize.height;
}
//TODO: for performance improvements https://github.com/blockai/phantom-pool
phantom.create().then(function (ph) {
ph.createPage().then(function (page) {
page.open(req.params.url).then(function (status) {
page.property('viewportSize',viewportSize);
page.property('clipRect', clipRect);
var image_file_name = url_to_process.replace(/\W/g, '_') + ".png";
var image_path = public_dir + "/" + image_file_name;
page.render(image_path).then(function () {
res.sendfile(image_path);
page.close();
ph.exit();
});
});
});
});
});
// start server
var server = app.listen(8080, function () {
console.log('Listening on port %d', server.address().port);
});