-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
139 lines (87 loc) · 2.71 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var express = require('express');
var app = express();
var path = require('path');
var fs = require('fs');
//var sleep = require('sleep');
//var routes = require('.api//routes');
var PythonShell = require('python-shell');
//var spawn = require('child_process').spawn;
//var py = spawn('python', ['sample.py']);
var bodyParser = require('body-parser');
app.set('port',3000);
app.use(express.static(path.join(__dirname,'public')));
app.use(bodyParser.urlencoded({ extended : true}));
app.use(bodyParser.json());
app.post('/', function(req, res)
{
var abs = req.body.abstract;
fs.writeFile('abstract.txt', abs, (err) => {
// throws an error, you could also catch it here
if (err) throw err;
// success case, the file was saved
console.log('abstract saved!');
});
var spawn = require('child_process').spawn;
var py=spawn('python', ['main.py']);
py.stdout.on('data',function(data,err){
if (err) throw err;
});
setTimeout(writeFunc(),50000);
function writeFunc()
{
console.log(data.toString());
res.write(data);
res.end('end');
}
//res.send(abs);
/*fs.readFile('output.txt', {encoding: 'utf-8'}, function(err,data){
if (!err) {
console.log('received data: ' + data);
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
} else {
console.log(err);
}
});
*/
});
/*PythonShell.run('/sample.py', function (err) {
if (err) throw err;
console.log('finished');
});*/
//app.use('/api',routes);
/*app.get('/',function(req,res){
console.log('get the homepage');
res.sendFile(path.join(__dirname,'public','index.html'));
});*/
app.get('/file',function(req,res){
console.log('get the file');
res.sendFile(path.join(__dirname,'app.js'));
});
app.listen(app.get('port'),function(){
console.log('hii');
});
/*var pyshell = new PythonShell('sample.py');
pyshell.send(JSON.stringify(abstract.txt))
pyshell.on('message', function (message) {
// received a message sent from the Python script (a simple "print" statement)
var jsondata = message
console.log(jsondata);
});
// end the input stream and allow the process to exit
pyshell.end(function (err) {
if (err)
throw err;
});*/
/* var options = {
mode: 'text',
pythonPath: 'sample.py',
pythonOptions: ['-u'],
scriptPath: '/',
args: ['abstract.txt']
};
PythonShell.run('sample.py', options, function (err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
console.log(results);
});*/