testnode/server.js

205 lines
7.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const http = require("http");
const { URL } = require("url");
const { Pool } = require("pg");
// Конфигурация Postgres, DATABASE_URL имеет приоритет.
function buildPgConfig() {
if (process.env.DATABASE_URL) {
return {
connectionString: process.env.DATABASE_URL,
ssl: process.env.PGSSLMODE === "require" ? { rejectUnauthorized: false } : undefined,
};
}
return {
host: process.env.PGHOST,
port: Number(process.env.PGPORT || "5432"),
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
database: process.env.PGDATABASE || "postgres",
ssl: process.env.PGSSLMODE === "require" ? { rejectUnauthorized: false } : undefined,
};
}
// Общий пул подключений.
const pool = new Pool(buildPgConfig());
// Таблица для демо создается автоматически.
async function ensureTable() {
await pool.query(
"CREATE TABLE IF NOT EXISTS nubes_test_table (id SERIAL PRIMARY KEY, test_data TEXT, created_at TIMESTAMP DEFAULT NOW())"
);
}
// Простой парсер application/x-www-form-urlencoded.
function parseForm(body) {
return body
.split("&")
.map((pair) => pair.split("="))
.reduce((acc, [key, value]) => {
acc[decodeURIComponent(key)] = decodeURIComponent((value || "").replace(/\+/g, " "));
return acc;
}, {});
}
// Рендер HTML-страницы с CRUD-формами.
function renderPage(rows, error) {
const errorHtml = error ? `<p style="color:red">${error}</p>` : "";
const rowsHtml = rows
.map(
(row) => `
<tr>
<td>${row.id}</td>
<td>
<form method="POST" action="/update">
<input type="hidden" name="id" value="${row.id}">
<input type="text" name="txt_content" value="${row.test_data}">
<button type="submit">save</button>
</form>
</td>
<td>
<form method="POST" action="/delete" onsubmit="return confirm('Delete?')">
<input type="hidden" name="id" value="${row.id}">
<button type="submit">delete</button>
</form>
</td>
</tr>`
)
.join("");
return `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>NodeJS + Postgres Demo</title>
<link rel="icon" href="https://nubes.ru/themes/custom/nubes/images/nubes-ico.svg" type="image/svg+xml">
<style>
:root { --nubes-blue: #005BFF; --nubes-dark: #1A1A1A; --nubes-grey: #F8F9FA; --nubes-border: #E5E7EB; }
body { font-family: 'Segoe UI', Tahoma, sans-serif; margin: 0; padding: 0; background: var(--nubes-grey); color: var(--nubes-dark); }
.header-bg { position: sticky; top: 0; z-index: 1000; background: #fff; border-bottom: 1px solid var(--nubes-border); padding: 15px 0; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
.container { max-width: 1000px; margin: auto; padding: 0 20px; }
.header-content { display: flex; align-items: center; justify-content: space-between; }
.logo { height: 40px; }
.main-content { padding: 40px 0; }
.card { background: #fff; padding: 32px; border-radius: 16px; box-shadow: 0 4px 20px rgba(0,0,0,0.04); }
.btn { display: inline-flex; align-items: center; justify-content: center; cursor: pointer; padding: 12px 24px; border: none; border-radius: 8px; font-weight: 600; font-size: 14px; }
.btn-primary { background: var(--nubes-blue); color: #fff; }
.btn-action { padding: 12px; background: #fff; border: 1px solid var(--nubes-border); border-radius: 8px; font-size: 18px; line-height: 1; cursor: pointer; min-width: 44px; }
.btn-action:hover { background: var(--nubes-grey); border-color: var(--nubes-blue); }
.input-group { display: flex; gap: 12px; margin-bottom: 32px; }
input[type="text"] { flex-grow: 1; width: 100%; padding: 12px 16px; border: 1px solid var(--nubes-border); border-radius: 8px; font-size: 14px; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; padding: 16px; font-size: 12px; text-transform: uppercase; color: #6B7280; border-bottom: 1px solid var(--nubes-border); }
td { padding: 16px; border-bottom: 1px solid var(--nubes-border); }
tbody tr:nth-child(even) { background-color: #FAFBFC; }
tbody tr:hover { background-color: #F3F4F6; }
.id-cell { font-family: monospace; color: #9CA3AF; width: 60px; }
.actions-cell { display: flex; gap: 12px; width: 130px; }
</style>
</head>
<body>
<div class="header-bg">
<div class="container header-content">
<img src="https://nubes.ru/themes/custom/nubes_2025/logo.svg" alt="Nubes" class="logo">
<div style="font-size: 14px; color: var(--nubes-blue); font-weight: 600;">NodeJS + Postgres Demo</div>
</div>
</div>
<div class="container main-content">
<div class="card">
${errorHtml}
<form method="POST" action="/add" class="input-group" onsubmit="const btn=this.querySelector('button'); if(btn){btn.disabled=true; btn.textContent='Adding...';}">
<input type="text" name="txt_content" placeholder="New message" required>
<button type="submit" class="btn btn-primary">Add</button>
</form>
<table>
<thead><tr><th>ID</th><th>Content</th><th>Actions</th></tr></thead>
<tbody>
${rowsHtml}
</tbody>
</table>
</div>
</div>
</body>
</html>`;
}
// Маршруты: /, /add, /update, /delete, /healthz.
async function handleRequest(req, res) {
const url = new URL(req.url, `http://${req.headers.host}`);
if (req.method === "GET" && url.pathname === "/healthz") {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("ok");
return;
}
if (req.method === "GET" && url.pathname === "/") {
try {
await ensureTable();
const { rows } = await pool.query(
"SELECT id, test_data FROM nubes_test_table ORDER BY id DESC LIMIT 20"
);
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
res.end(renderPage(rows));
} catch (err) {
res.writeHead(500, { "Content-Type": "text/html; charset=utf-8" });
res.end(renderPage([], err.message));
}
return;
}
if (req.method === "POST" && ["/add", "/update", "/delete"].includes(url.pathname)) {
let body = "";
req.on("data", (chunk) => {
body += chunk;
});
req.on("end", async () => {
const form = parseForm(body);
try {
await ensureTable();
if (url.pathname === "/add") {
const content = form.txt_content || "Node did it";
// Защита от быстрых дублей подряд.
const { rows: lastRows } = await pool.query(
"SELECT test_data, created_at FROM nubes_test_table ORDER BY id DESC LIMIT 1"
);
const last = lastRows[0];
const isDuplicate =
last &&
last.test_data === content &&
Date.now() - new Date(last.created_at).getTime() < 3000;
if (!isDuplicate) {
await pool.query("INSERT INTO nubes_test_table (test_data) VALUES ($1)", [content]);
}
}
if (url.pathname === "/update") {
await pool.query("UPDATE nubes_test_table SET test_data=$1 WHERE id=$2", [
form.txt_content || "",
form.id,
]);
}
if (url.pathname === "/delete") {
await pool.query("DELETE FROM nubes_test_table WHERE id=$1", [form.id]);
}
res.writeHead(303, { Location: "/" });
res.end();
} catch (err) {
res.writeHead(500, { "Content-Type": "text/html; charset=utf-8" });
res.end(renderPage([], err.message));
}
});
return;
}
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("Not found");
}
const port = process.env.PORT || 3000;
const server = http.createServer(handleRequest);
server.listen(port, () => {
console.log(`Server running on port ${port}`);
});