/
app.js
78 lines (60 loc) · 2.52 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
var cluster = require('cluster');
var numCPU = require('os').cpus().length;
if ( cluster.isMaster ){
for ( var i=0; i < numCPU; ++i ){
cluster.fork();
}
}
else {
var express = require('express'),
check = require('validator').check,
sanitize = require('validator').sanitize,
request = require('request'),
port = process.env.PORT || 5000;
var app = express();
app.use(express.logger());
app.use(express.compress()); //gzip compression
app.use(express.bodyParser()); //parse post uploads
app.use(express.static(__dirname + '/public'));
app.use(express.limit('500kb')); //limit uploads to 500kb
/*-----------------------------------------------------------------------------------*/
/* ##. ROUTES
/*-----------------------------------------------------------------------------------*/
app.post('/api/gethtml', express.bodyParser(), function(req, res){
if ( req.body.hstring ){
//res.set('Access-Control-Allow-Origin', '*');
//res.set('Access-Control-Allow-Headers', 'X-Requested-With');
var thehtml = req.body.hstring;
//sanitized_text = sanitize( thehtml ).escape(); // sanitize user entered text
res.set('Content-Disposition', 'attachment; filename=index.html');
res.type('text/html');
res.send(thehtml);
}
else {
res.send("Error: No code found!");
}
}); // end app.post
/*-----------------------------------------------------------------------------------*/
/* ##. SERVER LISTEN SETTINGS
/*-----------------------------------------------------------------------------------*/
app.listen(port, function(){
console.log("Listening on " + port);
});
/*-----------------------------------------------------------------------------------*/
/* ##. EXCEPTION CATCH-ALL (for uncaught exceptions)
/*-----------------------------------------------------------------------------------*/
//catch uncaught exceptions to prevent server shutdown
process.on('uncaughtException', function(e) {
console.log(e);
});
} // end of the cluster.isMaster logic
/*-----------------------------------------------------------------------------------*/
/* ##. GLOBALS
/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/* ##. GLOBAL EXCEPTION CATCH-ALL (for uncaught exceptions)
/*-----------------------------------------------------------------------------------*/
//catch uncaught exceptions to prevent server shutdown
process.on('uncaughtException', function(e) {
console.log(e);
});