-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_server2.js
79 lines (65 loc) · 2.68 KB
/
static_server2.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
var httpd = require("http").createServer(handler);
var io=require('/Users/user/Virtualenvs/node_modules/socket.io/lib/socket.io').listen(httpd);
var fs = require('fs');
var url = require("url");
var path = require("path");
var port = process.argv[2] || 8080;
var spawn = require('child_process').spawn;
httpd.listen(parseInt(port, 10));
function handler (request, response) {
var uri = url.parse(request.url).pathname,
filename = path.join(process.cwd(), uri);
console.log(uri);
path.exists(filename, function(exists) {
if(!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return; //these returns get you out of the function I think
}
if (fs.statSync(filename).isDirectory()) filename += '/index.html';
fs.readFile(filename, "binary", function(err, file) {
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
return;
}
response.writeHead(200);
response.write(file, "binary"); //otherwise here's where the file gets finally served
response.end();
}); //fs.readFile
}); //path.exists
}
io.sockets.on('connection',function(socket) {
socket.on('btn_api_call', function(argvlist) {
console.log('server says trying to emit callback');
socket.emit('serverMessage','client says trying to emit callback');
var kw_list="'error','problem','suck','issue','broken','wont','dont','cant','hate'"
var title_text='title_text'
var start_date='2013-08-14'
var end_date='2013-08-16'
var min='10'
var subdomain='hotel'
var notsubdomain='mobile'
var pythonarrays=['-i386','/Users/user/Virtualenvs/venv-p274/bin/python2.7','/Users/user/Documents/NLTKProject/newpythonolabparsing9.3.13.py']
var finalargs=pythonarrays.concat(argvlist);
console.log(finalargs);
var child= spawn('arch',finalargs)
// var child= spawn('arch',['-i386','/Users/user/Virtualenvs/venv-p274/bin/python2.7','/Users/user/Documents/NLTKProject/newpythonolabparsing9.1.13.py',kw_list,title_text,min,start_date,end_date,subdomain,notsubdomain])
child.on('exit',function(code,signal) {
if (code){
console.log('child process terminated with code' + code);
socket.emit('serverMessage','completed child process');
}
else if (signal) {
console.log('child process terminated because of signal' + signal);
}
else { console.log('some other result')
socket.emit('serverMessage','trying to reload');
socket.emit('jsonSuccess');
}
});
});
});
console.log("Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown");