-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
48 lines (41 loc) · 1.17 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
// Import necessary modules
const express = require('express');
const bodyParser = require('body-parser');
// Create an Express app
const app = express();
const port = 3000;
// Middleware to parse JSON requests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Set up a route to serve the HTML form
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
// Handle form submission
app.post('/calculate', (req, res) => {
const { num1, num2, operation } = req.body;
// Perform the calculation based on the operation
let result;
switch (operation) {
case 'add':
result = parseFloat(num1) + parseFloat(num2);
break;
case 'subtract':
result = parseFloat(num1) - parseFloat(num2);
break;
case 'multiply':
result = parseFloat(num1) * parseFloat(num2);
break;
case 'divide':
result = parseFloat(num1) / parseFloat(num2);
break;
default:
result = 'Invalid operation';
}
// Send the result back to the client
res.send(`Result: ${result}`);
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});