-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
57 lines (48 loc) · 1.65 KB
/
server.js
File metadata and controls
57 lines (48 loc) · 1.65 KB
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
var express = require('express');
var bodyParser = require('body-parser');
var phantom = require('phantom');
var spawn = require('child_process').spawn;
var fs = require('fs');
var screenshot = require('screenshot-stream');
var app = express();
app.use(bodyParser.json());
var public_dir = __dirname + '/public';
app.use(express.static(public_dir));
app.post('/process_url', function (req, res) {
var url_to_process = req.body.url;
if (!url_to_process) {
res.status(404).send({error: "please send correct url"})
} else {
var image_file_name = url_to_process.replace(/\W/g, '_') + "-" + (+new Date()) + ".png";
var image_path = public_dir + "/" + image_file_name;
var viewpostSize = req.body.viewport;
var stream = screenshot(url_to_process, viewpostSize || '1366x768');
stream.pipe(fs.createWriteStream(image_path));
stream.on('finish', function () {
res.send({url: image_path, name: image_file_name});
});
/* var prc = spawn('phantomjs', ['test.js', url_to_process, image_path ]);
prc.on('close', function(){
res.send({url: image_path, name: image_file_name});
});*/
/*
phantom.create(function (ph) {
ph.createPage(function (page) {
page.open(url_to_process, function (status) {
if (status == "success") {
page.render(image_path, function () {
res.send({url: image_path, name: image_file_name});
});
} else {
res.status(500).send({error: "internal error"})
}
page.close();
ph.exit();
});
});
});*/
}
});
var server = app.listen(3000, function() {
console.log('Listening..');
});