-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
213 lines (170 loc) · 6.3 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const http = require('http');
const fs = require('fs');
var myPythonScript = "SpellChecker.py";
var pythonExecutable = "python3";
const server = http.createServer((req, res) => {
if (req.method === 'POST') {
var body = '';
req.on('data', chunk => {
body += decodeURIComponent(chunk.toString());
});
req.on('end', () => {
fs.writeFileSync("input.txt", body);
//res.writeHeader(200, {"Content-Type": "text/html"});
var val = terminal();
if(val>0)
writing();
});
}
else {
res.end(`
<!doctype html>
<html>
<head>
<style type="text/css">
body {
background: black;
}
p {
color: lightgray;
font-family: Arial;
font-size: 50px;
}
form {
height: 400px;
width: 100%;
}
div {
margin-top: 5%;
text-align: center;
}
textarea {
border: 1px solid white;
border-radius: 5px;
width: 500px;
text-indent: 10px;
font-size: 15px;
padding-top: 10px;
}
button {
color : #f2f2f2;
width: 90px;
padding : 18px 18px;
background: #34a853 ;
border: 1px solid #34a853;
border-radius: 5px;
font-size: 15px;
font-family: Arial;
cursor: pointer;
margin-right: 5px;
}
button:hover {
background: #f2f2f2;
color: #34a853;
}
a {
text-decoration: none;
color : #f2f2f2;
width: 100px;
height: 300px;
padding : 18px 18px;
background: #e74c3c ;
border: 1px solid #e74c3c;
border-radius: 5px;
font-size: 15px;
font-family: Arial;
}
a:hover {
background : #f2f2f2;
color: #e74c3c;
}
</style>
</head>
<body>
<div>
<p>Spell Corrector<p>
<form action="/" method="post">
<!--<textarea rows="15" name="fname"></textarea>-->
<textarea rows="15" name="fname"></textarea>
<br/>
<br/>
<button>Submit</button>
<a href="http://localhost:8000">Output</a>
</form>
</div>
</body>
</html>
`);
}
});
server.listen(3000);
function terminal() {
// Function to convert an Uint8Array to a string
var uint8arrayToString = function(data){
return String.fromCharCode.apply(null, data);
};
const spawn = require('child_process').spawn;
const scriptExecution = spawn(pythonExecutable, [myPythonScript]);
// Handle normal output
scriptExecution.stdout.on('data', (data) => {
console.log(uint8arrayToString(data));
});
// Handle error output
scriptExecution.stderr.on('data', (data) => {
// As said before, convert the Uint8Array to a readable string.
console.log(uint8arrayToString(data));
});
scriptExecution.on('exit', (code) => {
console.log("Process quit with code : " + code);
});
return 1;
}
function writing() {
var text = "";
var result = "";
http.createServer(function (req,res) {
fs.readFile('mod_input.txt','utf8',function (err,data) {
text = data;
});
fs.readFile('output.txt', 'utf8', function (err, data) {
var arr = data.split(" ");
for(var i = 0;i < arr.length;i++) {
if(text.indexOf(arr[i]) === -1) {
result += "<"+"span"+">"+arr[i]+"<"+"/"+"span"+">"+" ";
}
else
result += arr[i] +" "
}
res.end(`
<!doctype html>
<html>
<head>
<style type="text/css">
body {
background: black;
}
#header {
color: #48c9b0;
font-size: 30px;
font-family: SansSerif;
}
#value {
color: white;
font-size: 20px;
}
span {
color: green;
}
</style>
</head>
<body>
<p id="header">INPUT</p>
<p id="value">${text}</p>
<br/>
<p id="header">OUTPUT</p>
<p id="value">${result}</p>
</body>
</html>`);
});
}).listen(8000);
}