Skip to content

Commit 5a85a77

Browse files
committed
added js code
1 parent 4b1926b commit 5a85a77

File tree

9 files changed

+97
-0
lines changed

9 files changed

+97
-0
lines changed

jsframework_app/.DS_Store

6 KB
Binary file not shown.

jsframework_app/backend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

jsframework_app/backend/server.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// server.js
2+
const express = require('express');
3+
const bodyParser = require('body-parser');
4+
const MongoClient = require('mongodb').MongoClient;
5+
6+
const app = express();
7+
app.use(bodyParser.json());
8+
9+
// Connect to MongoDB
10+
const client = new MongoClient('mongodb://localhost:27017/');
11+
let db;
12+
client.connect(err => {
13+
db = client.db('mydatabase');
14+
});
15+
16+
app.post('/', (req, res) => {
17+
const num1 = parseFloat(req.body.num1);
18+
const num2 = parseFloat(req.body.num2);
19+
const result = num1 + num2;
20+
21+
// Store the numbers and result in the database
22+
db.collection('results').insertOne({ num1, num2, result }, (err, r) => {
23+
res.send({ result });
24+
});
25+
});
26+
27+
app.listen(3000, () => console.log('Server running on port 3000'));

jsframework_app/frontend/src/App.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// App.js
2+
import React, { useState } from 'react';
3+
import axios from 'axios';
4+
5+
function App() {
6+
const [num1, setNum1] = useState('');
7+
const [num2, setNum2] = useState('');
8+
const [result, setResult] = useState(null);
9+
10+
const addNumbers = async () => {
11+
const response = await axios.post('http://localhost:3000/', { num1, num2 });
12+
setResult(response.data.result);
13+
};
14+
15+
return (
16+
<div>
17+
<input type="number" value={num1} onChange={e => setNum1(e.target.value)} />
18+
<input type="number" value={num2} onChange={e => setNum2(e.target.value)} />
19+
<button onClick={addNumbers}>Add</button>
20+
{result && <p>Result: {result}</p>}
21+
</div>
22+
);
23+
}
24+
25+
export default App;

jsframework_app/frontend/src/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import App from './App';
4+
5+
ReactDOM.render(
6+
<React.StrictMode>
7+
<App />
8+
</React.StrictMode>,
9+
document.getElementById('root')
10+
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

jsframework_app/public/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1" />
6+
<title>My App</title>
7+
</head>
8+
<body>
9+
<div id="root"></div>
10+
</body>
11+
</html>

python_app/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use an official Python runtime as a parent image
2+
FROM python:3.8-slim-buster
3+
4+
# Set the working directory in the container to /app
5+
WORKDIR /app
6+
7+
# Add the current directory contents into the container at /app
8+
ADD . /app
9+
10+
# Install any needed packages specified in requirements.txt
11+
RUN pip install --no-cache-dir -r requirements.txt
12+
13+
# Make port 5000 available to the world outside this container
14+
EXPOSE 5000
15+
16+
# Define environment variable
17+
ENV NAME World
18+
19+
# Run app.py when the container launches
20+
CMD ["python", "add_numbers_with_db.py"]

python_app/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
flask==1.1.2
2+
pymongo==3.11.0

0 commit comments

Comments
 (0)