forked from xk/nodeSnippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsideNode.js
71 lines (64 loc) · 2.43 KB
/
insideNode.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
#!/usr/bin/env node
//201003xx [email protected]
var sys= require("sys");
var http= require("http");
var port= 12345;
function insideNode (n, hits) {
hits= 0;
return function (request, response) {
var body= "", pre= "", props= [];
var time= +new Date();
function expandir (o, nombre, prefijo, sufijo) {
var n, linea;
var tipo= typeof o;
if (tipo === "object") {
if (({}).toString.call(o).toLowerCase().indexOf('array') >= 0) {
tipo= "array";
}
}
if ((tipo === "object") || (tipo === "function") || (tipo === "array")) {
n= props.indexOf(o);
if (n >= 0) {
//Ya se ha expandido anteriormente
linea= "<td><a href=\"#o"+ n+ "\">."+ nombre+ "</a></td><td>["+ tipo+ "]: "+ o+ "</td>";
body+= "<tr>"+ prefijo+ linea+ sufijo+ "</tr>";
return;
}
n= props.push(o)- 1;
linea= "<td id=\"o"+ n+ "\">."+ nombre+ "</td><td>["+ tipo+ "]: "+ o+ "</td>";
body+= "<tr>"+ prefijo+ linea+ sufijo+ "</tr>";
linea= "<td><a href=\"#o"+ n+ "\">."+ nombre+ "</a></td>";
try {
var keys= Object.getOwnPropertyNames(o);
} catch (e) {
keys= [];
}
keys.sort(function (a,b) {
return 1- (2* (a < b));
});
for (var q in keys) {
expandir(o[keys[q]], keys[q], prefijo+ linea, sufijo);
}
} else {
linea= "<td>."+ nombre+ "</td><td>["+ tipo+ "]: "+ o+ "</td>";
body+= "<tr>"+ prefijo+ linea+ sufijo+ "</tr>";
}
}
if (request.url === "/favicon.ico") {
response.writeHeader(404, {});
} else {
hits++;
response.writeHeader(200, {"Content-Type": "text/html", "server":"Node.js"});
var pre= "<html>\n<head>\n<style type=\"text/css\">\ntd {\n font-family:monospace;\n border:1px solid red;\n}\ntable {\n margin-top:30px;\n width:100%;\n }\n</style>\n<title>JorgeChamorro Node.js Server</title>\n</head>\n<body>\n<h1>";
body= "</h1><br>\n<table>\n";
expandir((function(){return this;})(), "global", "\n", "");
body+= "\n</table>\n</body>\n</html>";
}
var txt= "puerto:"+ n + ", hits:"+ hits+ ", url:"+ request.url+ ", tiempo:"+ (((+ new Date())-time)/1000).toFixed(3)+ "s";
response.write(pre+ txt+ body);
response.close();
//sys.puts(txt);
};
}
http.createServer(insideNode(port)).listen(port);
sys.puts("Server running at http://localhost:"+ port+ "/");