-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (59 loc) · 1.79 KB
/
index.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
const express = require('express');
const cors = require('cors'); // Füge diese Zeile hinzu
const app = express();
let port = 3000;
app.use(cors()); // Aktiviere CORS
app.get('/add/:num1/:num2', (req, res) => {
const num1 = parseFloat(req.params.num1);
const num2 = parseFloat(req.params.num2);
let result = "";
if (isNaN(num1) || isNaN(num2)) {
res.status(400).json({ error: 'Invalid Data Type' });
} else {
result = num1 + num2;
res.json({ result });
}
});
app.get('/sub/:num1/:num2', (req, res) => {
performOperation(req, res, '-');
});
app.get('/mul/:num1/:num2', (req, res) => {
performOperation(req, res, '*');
});
app.get('/div/:num1/:num2', (req, res) => {
performOperation(req, res, '/');
});
function performOperation(req, res, operation) {
const num1 = parseFloat(req.params.num1);
const num2 = parseFloat(req.params.num2);
let result = "";
if (isNaN(num1) || isNaN(num2)) {
res.status(400).json({ error: 'Invalid Data Type' });
} else {
switch (operation) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 === 0) {
res.status(400).json({ error: 'Division by zero is not allowed' });
return;
}
result = num1 / num2;
break;
default:
res.status(400).json({ error: 'Invalid Operation' });
return;
}
res.json({ result });
}
}
app.listen(port, () => {
console.log(`Taschenrechner API started on port ${port}`);
});