-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestHandlers.js
84 lines (63 loc) · 2.49 KB
/
requestHandlers.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
var fs = require('fs');
var querystring = require("querystring");
var XMLHttpRequest = require("w3c-xmlhttprequest").XMLHttpRequest
var pug = require('pug')
function iniciar(response, postData) {
console.log("Manipulador de peticion 'inicio' fue llamado.");
var f = 'index.html';
fs.readFile(f, function (err, data) {
if(err){
response.writeHead(404,{'Content-type':'text/html'});
return response.end()
}else{
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data);
response.end();
}
});
}
function subir(response, dataPosteada) {
console.log("Manipulador de peticion 'subir' fue llamado.");
response.writeHead(200, {"Content-Type": "text/html"});
var siteId = querystring.parse(dataPosteada)["selectSites"];
const categoryId = querystring.parse(dataPosteada)["selectCategories"];
const filas = querystring.parse(dataPosteada)["filas"];
const columnas = querystring.parse(dataPosteada)["columnas"];
var request = new XMLHttpRequest();
var target = 'https://api.mercadolibre.com/trends/'+ siteId + '/' + categoryId
request.open('GET', target, true);
request.onload = function () {
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />'+
'<style> table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; }'+
'td, th { border: 1px solid #dddddd; text-align: left; padding: 8px;}'+
' tr:nth-child(even) { background-color: #dddddd;} </style>'+
'</head>'+
'<body>'+
'<table>'
// Begin accessing JSON data here
var data = JSON.parse(this.response);
if (request.status >= 200 && request.status < 400) {
var cont = 0;
for (var i=0; i < filas; i++) {
body += '<tr>'
for(var j=0; j < columnas; j++){
body += '<td>'+data[cont].keyword+'</td>'
cont++
}
body += '</tr>'
}
body += '</table></body></html>'
} else {
const errorMessage = document.createElement('marquee');
errorMessage.textContent = "No funciona!";
app.appendChild(errorMessage);
}
response.write(body)
response.end();
}
request.send();
}
exports.iniciar = iniciar;
exports.subir = subir;