-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathapp.js
More file actions
116 lines (100 loc) · 4.17 KB
/
app.js
File metadata and controls
116 lines (100 loc) · 4.17 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//File name: app.js
//Description: the entry point to the chat app server
//Last updated: 4/28/2019
// Name of .jar file for BloomingLeaf project must be Blooming.jar
var userPath = "<your absolute path here>/BloomingLeaf"
var http = require('http'),
url = require('url'),
fileServer = require('./leaf-analysis/node/fileServer.js'),
fs = require('fs');
qs = require('./leaf-analysis/node/query.js'),
utils = require('./leaf-analysis/node/utils.js');
exec = require('child_process').exec;
//TODO: If wait is not longer needed, this function can be removed.
function wait(ms){
var start = new Date().getTime();
var end = start;
while(end < start + ms) {
end = new Date().getTime();
}
}
function processGet(path,queryObj,res) {
// if URL includes a path to a file, call file server method
if (path && path.length > 1) {
// precede the given path with name of the subdirectory in which
// the client files are stored
fileServer.serve_static_file(userPath+"/leaf-ui"+path,res);
}
// check if there is a query. The query object will always be created
// even if there is no quesry string. So check using a property that you
// know will be part of any query sent by the client. This is application
// specific - depends on our knowledge of what the client will serve
// alternatively - you can check if (url.parse(req.url).search)
// search is the query string
/*else if (queryObj.request) {
qs.processQuery(queryObj,res);
} else {
// if not file path or query were sent - read and serve default html file
fileServer.serve_static_file("public_html/index.html",res);
fileServed = true;
}*/
}
function processPost(queryObj,req,res) {
var body = '';
req.on('data', data => {body += data;}); // get the request data
req.on('end', () => { // request fully received - process it
if (body || queryObj.name) {
queryObj.message = body; // specific to the chat application
qs.processQuery(queryObj,res);
}
fs.writeFileSync(userPath+"/leaf-analysis/temp/default.json",body);
passIntoJar(res);
// //TODO: Can this function be written in an asynchronous call?
// wait(1000);
// //TODO: Can this be made asynchronous by moving the follow code down to to "HERE"
// //read from output.out and pass the data as a string with the response
// analysisFile = fs.readFileSync(userPath+"/leaf-analysis/temp/output.out");
// analysisFileString = String(analysisFile);
// res.writeHead(200, { "Content-Type" : 'text/plain'});
// // send data
// res.write(analysisFileString);
// res.end();
});
}
// server call back function
function handle_incoming_request(req, res) {
// get the path of the file to served
var path = url.parse(req.url).pathname;
// get a query (true makes sure the query string is parsed into an object)
var queryObj = url.parse(req.url,"true").query;
if (req.method.toLowerCase() == "get")
processGet(path,queryObj,res);
else if (req.method.toLowerCase() == "post")
processPost(queryObj,req,res);
else
utils.sendText(res,400,"Server accepts only GET ans POST requests");
}
/*
*This function executed the jar file
*/
function passIntoJar(res) {
child = exec('java -jar '+userPath+'/leaf-analysis/bin/Blooming.jar ',
function (error, stdout, stderr){
if(error !== null){
console.log('exec error: ' + error);
}
else{
//Analysis return code.
analysisFile = fs.readFileSync(userPath+"/leaf-analysis/temp/output.out");
analysisFileString = String(analysisFile);
res.writeHead(200, { "Content-Type" : 'text/plain'});
// send data
res.write(analysisFileString);
res.end();
return stdout;
}
});
return child;
}
var server = http.createServer(handle_incoming_request);
server.listen(8080);