kapliuk
This commit is contained in:
parent
5b164b53ad
commit
437a05ee15
9
z1/backend/Dockerfile
Normal file
9
z1/backend/Dockerfile
Normal file
@ -0,0 +1,9 @@
|
||||
FROM node:18
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY server.js .
|
||||
|
||||
RUN npm init -y && npm install express pg cors
|
||||
|
||||
CMD ["node", "server.js"]
|
||||
44
z1/backend/server.js
Normal file
44
z1/backend/server.js
Normal file
@ -0,0 +1,44 @@
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
const { Pool } = require('pg');
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
const pool = new Pool({
|
||||
host: 'db',
|
||||
user: 'user',
|
||||
password: 'password',
|
||||
database: 'mydb',
|
||||
port: 5432
|
||||
});
|
||||
|
||||
pool.query(`
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name TEXT
|
||||
)
|
||||
`);
|
||||
|
||||
app.post('/save', async (req, res) => {
|
||||
const name = req.body.name;
|
||||
|
||||
try {
|
||||
await pool.query('INSERT INTO users(name) VALUES($1)', [name]);
|
||||
res.send("Saved to DB: " + name);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).send("Error");
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/users', async (req, res) => {
|
||||
const result = await pool.query('SELECT * FROM users');
|
||||
res.json(result.rows);
|
||||
});
|
||||
|
||||
app.listen(3000, () => {
|
||||
console.log('Backend running on port 3000');
|
||||
});
|
||||
41
z1/docker-compose.yaml
Normal file
41
z1/docker-compose.yaml
Normal file
@ -0,0 +1,41 @@
|
||||
services:
|
||||
db:
|
||||
image: postgres:15
|
||||
volumes:
|
||||
- db_data:/var/lib/postgresql/data
|
||||
environment:
|
||||
POSTGRES_USER: user
|
||||
POSTGRES_PASSWORD: password
|
||||
POSTGRES_DB: mydb
|
||||
restart: always
|
||||
|
||||
adminer:
|
||||
image: adminer
|
||||
restart: always
|
||||
ports:
|
||||
- "8081:8080"
|
||||
depends_on:
|
||||
- db
|
||||
|
||||
web:
|
||||
image: nginx:latest
|
||||
restart: always
|
||||
ports:
|
||||
- "8080:80"
|
||||
volumes:
|
||||
- ./frontend:/usr/share/nginx/html
|
||||
|
||||
backend:
|
||||
build: ./backend
|
||||
restart: always
|
||||
ports:
|
||||
- "5000:3000"
|
||||
depends_on:
|
||||
- db
|
||||
environment:
|
||||
DB_HOST: db
|
||||
DB_USER: user
|
||||
DB_PASSWORD: password
|
||||
DB_NAME: mydb
|
||||
volumes:
|
||||
db_data:
|
||||
49
z1/frontend/index.html
Normal file
49
z1/frontend/index.html
Normal file
@ -0,0 +1,49 @@
|
||||
<h1>Save name to DB</h1>
|
||||
|
||||
<input id="name" placeholder="Enter name">
|
||||
<button onclick="sendAndLoad()">Save & Show</button>
|
||||
|
||||
<ul id="list"></ul>
|
||||
|
||||
<script>
|
||||
async function loadUsers() {
|
||||
try {
|
||||
const res = await fetch("http://localhost:5000/users");
|
||||
const data = await res.json();
|
||||
|
||||
const list = document.getElementById("list");
|
||||
list.innerHTML = "";
|
||||
|
||||
data.forEach(u => {
|
||||
const li = document.createElement("li");
|
||||
li.innerText = u.name;
|
||||
list.appendChild(li);
|
||||
});
|
||||
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
async function sendAndLoad() {
|
||||
const name = document.getElementById("name").value;
|
||||
|
||||
try {
|
||||
await fetch("http://localhost:5000/save", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({ name })
|
||||
});
|
||||
|
||||
await loadUsers();
|
||||
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
alert("Error");
|
||||
}
|
||||
}
|
||||
|
||||
window.onload = loadUsers;
|
||||
</script>
|
||||
Loading…
Reference in New Issue
Block a user