-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
92 lines (82 loc) · 3 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
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
const path = require('path');
const express = require('express');
const request = require('request');
const bodyParser = require('body-parser');
module.exports = {
app: function () {
const app = express();
const indexPath = path.join(__dirname, 'index.html');
const publicPath = express.static(path.join(__dirname, '/dist'));
const postReqOptions = {
url: 'https://api.tinify.com/shrink',
method: 'POST',
headers: {
'Authorization': 'Basic YXBpOkI3ODVqTHpHekQ1cjF0QlpJLW9sdjFQR3VHX1dBYVIw',
'Content-type': 'text/plain'
}
}
// const getReqOptions = {
// url: 'https://api.tinify.com/output',
// method: 'GET',
// headers: {
// 'Authorization': 'Basic YXBpOkI3ODVqTHpHekQ1cjF0QlpJLW9sdjFQR3VHX1dBYVIw',
// 'Content-type': 'text/plain'
// }
// }
app.use(bodyParser.raw({limit: '50mb', type: 'application/octet-stream'}));
app.post('/post', function(req, resp) {
console.log("REQUEST BODY: ", req.body);
function callback(error, response, body) {
console.log("BODY: ", body);
//console.log("ERROR: ", error);
//console.log("RESPONSE: ", response);
//console.log("RESPONSE: ", response.statusCode);
if(!error && response.statusCode >= 200 && response.statusCode <= 300) {
var info = JSON.parse(body);
resp.status(200).send(info);
}
}
request.post({
url: postReqOptions.url,
headers: postReqOptions.headers,
body: req.body
}, callback);
});
// app.get('/download', function(req, resp) {
// //console.log(req);
// //console.log(resp);
// //console.log("GET REQUEST QUERY STRING: ", req.query.imageUrl);
// function callback(error, response, body) {
// //console.log("BODY: ", body);
// //console.log("BODY INPUT: ", req.url);
// //console.log("ERROR: ", error);
// //console.log("RESPONSE: ", response.headers);
// if(!error && response.statusCode >= 200 && response.statusCode <= 300) {
// const info = req.query.imageUrl;
// console.log(info);
// // const binary = new Buffer.from(body, 'binary');
// // console.log(binary);
// // const type = response.headers['content-type'],
// // prefix = `data:${type};base64,`,
// // base64 = binary.toString('base64'),
// // data = `${prefix}${base64}`,
// // obj = {
// // "src": req.query.imageUrl,
// // "type": type,
// // "data": data
// // };
// resp.status(200).send(info);
// }
// }
// request.get({
// url: req.query.imageUrl,
// //headers: getReqOptions.headers,
// }, callback);
// });
app.use('/dist', publicPath);
app.get('*', function (request, response){
response.sendFile(path.resolve(__dirname, 'index.html'))
});
return app;
}
};