-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
120 lines (99 loc) · 3.13 KB
/
server.js
File metadata and controls
120 lines (99 loc) · 3.13 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
117
118
119
120
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
const cors = require('cors');
const port = process.env.APP_PORT ?? '4000';
var fs = require("fs");
const { exec } = require("child_process");
app.use(cors());
app.post('/add', (req, res) => {
let args = req.body;
const [operandOne, operandTwo] = [Number(args['operandOne']), Number(args['operandTwo'])];
const regex = /^0+/i;
console.log(`Adding ${operandOne} and ${operandTwo}`);
fs.writeFileSync("input.txt",operandOne.toString() + '\n');
fs.appendFileSync("input.txt",operandTwo.toString());
/*
fs.writeFileSync("input.txt","");
var MaxL = operandOne.length;
if (operandOne.length < operandTwo.length) {
MaxL = operandTwo.length;
}
if ((operandOne.length - operandTwo.length) > 0)
{
var newT = "";
for (let i = 0; i < (operandOne.length - operandTwo.length); i++) {
newT += "0";
}
newT += operandTwo;
fs.appendFileSync("input.txt", operandOne);
fs.appendFileSync("input.txt", newT);
} else {
var newT = "";
for (let i = 0; i < (operandTwo.length - operandOne.length); i++) {
newT += "0";
}
newT += operandOne;
fs.appendFileSync("input.txt", newT);
fs.appendFileSync("input.txt", operandTwo);
}
*/
console.log(`wrote to input.txt`);
exec("./cobolApp", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
res.end( `error: ${error.message}` );
}
if (stderr) {
console.log(`stderr: ${stderr}`);
res.end( `error: ${stderr}` );
}
console.log(`stdout: ${stdout}`);
fs.readFile( __dirname + "/" + "output.txt", 'utf8', function (err, data) {
console.log(`got output.txt`);
console.log( data.toString() );
//res.end( data );
// trim preceeding 0s
res.send(data.toString().replace(regex, ''));
});
//res.end( `${stdout}` );
});
//let result = operandOne / operandTwo;
// res.send(result.toString());
});
app.listen(port, () => console.log(`Listening on port ${port}!`));
/*
var express = require('express');
var app = express();
app.get('/runcobol', function (req, res) {
exec("./cobolApp", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
res.end( `error: ${error.message}` );
}
if (stderr) {
console.log(`stderr: ${stderr}`);
res.end( `error: ${stderr}` );
}
console.log(`stdout: ${stdout}`);
res.end( `${stdout}` );
});
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
var operandTest = "123456789";
var operandTttt = "4321";
var newT = "";
console.log(`${operandTest.length - operandTttt.length}`);
console.log(`${operandTttt.length}`);
for (let i = 0; i < (operandTest.length - operandTttt.length); i++) {
newT += "0";
}
newT += operandTttt;
console.log(newT);
console.log(operandTest);
console.log("Example app listening at http://%s:%s", host, port)
})
*/