-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
66 lines (59 loc) · 1.71 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
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
const express = require('express');
const app = express();
var fs = require('fs')
var path = require('path')
var bodyParser = require('body-parser')
var request = require('request');
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname,'./public')));
app.get('/',(req,res)=>{
res.sendFile(path.join(__dirname,'./public/form.html'));
res.end()
});
app.post('/add',(req,res)=>{
console.log(req.body.url,req.body.width,req.body.height,req.body.full,req.body.fresh)
if(req.body.full=='on')
{
console.log('true')
const full=true;
}else{
console.log('false')
const full=false;
}
if(req.body.fresh=='on')
{
console.log('true')
const fresh=true;
}else{
console.log('false')
const fresh=false;
}
// The parameters.
var token = 'YOUR_TOKEN_PROVIDED_BY_API';
var url = encodeURIComponent(req.body.url);
var width = req.body.width;
var height = req.body.width;
var output = 'image';
// Create the query URL.
var query = "https://screenshotapi.net/api/v1/screenshot";
query += `?token=${token}&full_page=${req.body.full=='on'}&fresh=${req.body.fresh=='on'}&url=${url}&width=${width}&height=${height}&output=${output}`;
// Call the API and save the screenshot.
request.get({url: query, encoding: 'binary'}, (err, response, body) => {
fs.writeFile("screenshot.png", body, 'binary', err => {
if (err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
});
res.send(req.body.url)
res.end()
})
let port = process.env.PORT;
if (port == null || port == "") {
port = 8000;
}
app.listen(port,()=>{
console.log('server running')
})