-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
99 lines (76 loc) · 2.57 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
93
94
95
96
97
98
99
var express = require('express');
var CloudmersiveVirusApiClient = require('cloudmersive-virus-api-client');
var formidable = require('formidable');
var firebase = require('firebase');
//Insert firebase config
var firebaseApp = firebase.initializeApp(firebaseConfig);
var fs = require('fs');
var app = express();
app.use(express.static(__dirname + '/public')); //__dir and not _dir
var port = 8000; // you can use any port
app.listen(port);
console.log('server on' + port);
app.use(express.json());
//Translation
app.post('/apitranslation', (request, response) => {
console.log(request.body);
const data = request.body;
var clientId = "FREE_TRIAL_ACCOUNT";
var clientSecret = "PUBLIC_SECRET";
var fromLang = data.fromLang;
var toLang = data.toLang;
var text = data.message;
var jsonPayload = JSON.stringify({
fromLang: fromLang,
toLang: toLang,
text: text
});
var options = {
hostname: "api.whatsmate.net",
port: 80,
path: "/v1/translation/translate",
method: "POST",
headers: {
"Content-Type": "application/json",
"X-WM-CLIENT-ID": clientId,
"X-WM-CLIENT-SECRET": clientSecret,
"Content-Length": Buffer.byteLength(jsonPayload)
}
};
var translateRequest = new http.ClientRequest(options);
translateRequest.end(jsonPayload);
var translatedMessage = 'Untranslated';
translateRequest.on('response', function (translateResponse) {
console.log('Status code: ' + translateResponse.statusCode);
translateResponse.setEncoding('utf8');
translateResponse.on('data', function (chunk) {
console.log(chunk);
translatedMessage = chunk;
response.json({
translatedMessage: translatedMessage
});
})
})
});
app.post('/apiviruscheck', (request,response) => {
var fileToCheck = '';
new formidable.IncomingForm().parse(request, (err, fields, files) => {
if (err) {
console.error('Error', err)
throw err
}
var defaultClient = CloudmersiveVirusApiClient.ApiClient.instance;
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = "";
var api = new CloudmersiveVirusApiClient.ScanApi();
var inputFile = fs.readFileSync(files.document.path);
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
api.scanFile(Buffer.from(inputFile.buffer), callback);
})
});