init: Node.js CRUD v1.0.0 — Express + PG, same table, Nubes design
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* server.js — Node.js CRUD (Express + PostgreSQL).
|
||||
*
|
||||
* Переменные окружения:
|
||||
* PGHOST, PGPORT, PGDATABASE, PGUSER, PGPASSWORD
|
||||
* TABLE_NAME (по умолчанию crud_items)
|
||||
* PORT (по умолчанию 3000)
|
||||
*
|
||||
* При старте: CREATE TABLE IF NOT EXISTS.
|
||||
*/
|
||||
const express = require("express");
|
||||
const { Pool } = require("pg");
|
||||
const path = require("path");
|
||||
|
||||
const VERSION = "1.0.0";
|
||||
const TABLE_NAME = process.env.TABLE_NAME || "crud_items";
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
const pool = new Pool({
|
||||
host: process.env.PGHOST || "127.0.0.1",
|
||||
port: parseInt(process.env.PGPORT || "5432"),
|
||||
database: process.env.PGDATABASE || "postgres",
|
||||
user: process.env.PGUSER || "postgres",
|
||||
password: process.env.PGPASSWORD || "",
|
||||
});
|
||||
|
||||
const app = express();
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
app.use(express.static(path.join(__dirname, "public")));
|
||||
app.set("view engine", "ejs");
|
||||
app.set("views", path.join(__dirname, "views"));
|
||||
|
||||
// --- Инициализация БД ---
|
||||
async function initDB() {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (
|
||||
id SERIAL PRIMARY KEY,
|
||||
value TEXT NOT NULL DEFAULT ''
|
||||
)
|
||||
`);
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
// --- Главная страница (GET + POST) ---
|
||||
app.get("/", async (req, res) => {
|
||||
try {
|
||||
let editRow = null;
|
||||
if (req.query.edit) {
|
||||
const result = await pool.query(
|
||||
`SELECT id, value FROM ${TABLE_NAME} WHERE id = $1`,
|
||||
[parseInt(req.query.edit)]
|
||||
);
|
||||
if (result.rows.length > 0) editRow = result.rows[0];
|
||||
}
|
||||
|
||||
const result = await pool.query(
|
||||
`SELECT id, value FROM ${TABLE_NAME} ORDER BY id`
|
||||
);
|
||||
|
||||
res.render("index", {
|
||||
table_name: TABLE_NAME,
|
||||
rows: result.rows,
|
||||
editRow,
|
||||
version: VERSION,
|
||||
});
|
||||
} catch (err) {
|
||||
res.status(500).send(err.message);
|
||||
}
|
||||
});
|
||||
|
||||
app.post("/", async (req, res) => {
|
||||
try {
|
||||
const { action, value, id } = req.body;
|
||||
|
||||
if (action === "create") {
|
||||
await pool.query(`INSERT INTO ${TABLE_NAME} (value) VALUES ($1)`, [value || ""]);
|
||||
} else if (action === "update") {
|
||||
await pool.query(`UPDATE ${TABLE_NAME} SET value = $1 WHERE id = $2`, [
|
||||
value || "",
|
||||
parseInt(id || 0),
|
||||
]);
|
||||
} else if (action === "delete") {
|
||||
await pool.query(`DELETE FROM ${TABLE_NAME} WHERE id = $1`, [
|
||||
parseInt(id || 0),
|
||||
]);
|
||||
}
|
||||
|
||||
res.redirect("/");
|
||||
} catch (err) {
|
||||
res.status(500).send(err.message);
|
||||
}
|
||||
});
|
||||
|
||||
// --- Старт ---
|
||||
initDB().then(() => {
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Node.js CRUD v${VERSION} — http://0.0.0.0:${PORT}`);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user