40 lines
983 B
JavaScript
40 lines
983 B
JavaScript
import "dotenv/config";
|
|
import express from "express";
|
|
import { Client } from "pg";
|
|
|
|
// Legacy single-file implementation kept for comparison with the layered src/ structure.
|
|
const client = new Client({
|
|
host: process.env.PGHOST,
|
|
port: Number(process.env.PGPORT),
|
|
database: process.env.PGDATABASE,
|
|
user: process.env.PGUSER,
|
|
password: process.env.PGPASSWORD,
|
|
});
|
|
|
|
const app = express();
|
|
|
|
app.get("/dinosaurs", async (_request, response) => {
|
|
const result = await client.query("SELECT * FROM dinosaur ORDER BY id");
|
|
const dinosaurs = result.rows.map((row) => ({
|
|
id: row.id,
|
|
name: row.name,
|
|
description: row.description,
|
|
period: row.period,
|
|
wikipediaAddress: row.wikipedia_address,
|
|
}));
|
|
response.json(dinosaurs);
|
|
});
|
|
|
|
await client.connect();
|
|
console.log("Connected to database");
|
|
|
|
app.listen(3000, () => {
|
|
console.log("Server running on port 3000");
|
|
});
|
|
|
|
process.on("SIGINT", async () => {
|
|
console.log("Shutting down...");
|
|
await client.end();
|
|
process.exit(0);
|
|
});
|