-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
78 lines (60 loc) · 2.21 KB
/
app.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
var express = require('express');
var https = require('https');
var multer = require('multer');
var storage = multer.memoryStorage();
var upload = multer({ storage: storage });
var app = express();
var server = require('http').createServer(app);
//app.use(express.static('public'))
app.use(express.static(__dirname + '/public'));
app.get('/', function () {})
var port = process.env.PORT || '3000';
server.listen(port, function () {
console.log('Glitch app listening on port 3000!')
});
//app.listen(port, function () {
// console.log('Glitch app listening on port 3000!')
//})
//Post request when user uploads a file
app.post('/upload', upload.single('file'), function (req, res, next) {
console.log(req.body.type);
//Data is the JSON object sent to the Azure function. Will contain the type of glitch and picture.
var data = {"type": req.body.type, "base64Picture": req.file.buffer.toString('base64')};
var options = {
hostname: 'glitchfunction.azurewebsites.net',
port: 443,
path: '/api/GlitchWebhookCsharp?code=AaiiPgczD04wrwJpC0taN4oZdpD1D/bPFZtIfIlyekU4gBnjwZWK4g==',
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
};
//The actual http request sent to azure functions
var funcreq = https.request(options, function(funcres) {
console.log('STATUS: ' + funcres.statusCode);
console.log('HEADERS: ' + JSON.stringify(funcres.headers));
funcres.setEncoding('utf8');
//Get back the glitched picture from azure functions
var picData = "";
funcres.on('data', function (chunk) {
picData += chunk;
});
funcres.on('end', function(){
var jsonResponse = JSON.parse(picData);
if (jsonResponse.base64Picture) {
console.log('got image back from azure function');
// send json response back to front end client
res.send({image: jsonResponse.base64Picture});
} else {
// oops something went wrong
res.status(500).send({error: "we could not get your pic glitched, sorry :("});
}
})
});
funcreq.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
funcreq.write(JSON.stringify(data));
funcreq.end();
});