chore: migrate to node.js backend to support database saving and serve static files
Deploy TrassenPlaner / deploy (push) Successful in 10s
Details
Deploy TrassenPlaner / deploy (push) Successful in 10s
Details
This commit is contained in:
parent
4107a62167
commit
2b1eefe229
2
app.js
2
app.js
|
|
@ -1434,7 +1434,7 @@ document.addEventListener('DOMContentLoaded', async () => {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('http://localhost:3000/api/wea', {
|
const response = await fetch('/api/wea', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ projekt_id, turbines: turbineData })
|
body: JSON.stringify({ projekt_id, turbines: turbineData })
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
{
|
||||||
|
"name": "bwsamern-ohne-planungstool",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"main": "server.js",
|
||||||
|
"dependencies": {
|
||||||
|
"body-parser": "^1.20.2",
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"dotenv": "^16.4.5",
|
||||||
|
"express": "^4.19.2",
|
||||||
|
"pg": "^8.11.5"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"start": "node server.js"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,6 +3,7 @@ const express = require('express');
|
||||||
const { Pool } = require('pg');
|
const { Pool } = require('pg');
|
||||||
const bodyParser = require('body-parser');
|
const bodyParser = require('body-parser');
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = process.env.PORT || 3000;
|
const port = process.env.PORT || 3000;
|
||||||
|
|
@ -10,6 +11,9 @@ const port = process.env.PORT || 3000;
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
|
// Serve static files from the root directory
|
||||||
|
app.use(express.static(path.join(__dirname, './')));
|
||||||
|
|
||||||
const pool = new Pool({
|
const pool = new Pool({
|
||||||
host: process.env.DB_HOST,
|
host: process.env.DB_HOST,
|
||||||
port: process.env.DB_PORT,
|
port: process.env.DB_PORT,
|
||||||
|
|
@ -20,7 +24,7 @@ const pool = new Pool({
|
||||||
|
|
||||||
// Test DB Connection
|
// Test DB Connection
|
||||||
pool.connect()
|
pool.connect()
|
||||||
.then(() => console.log('Connected to PostgreSQL (enwelo_db) successfully'))
|
.then(() => console.log('Connected to PostgreSQL successfully'))
|
||||||
.catch(err => console.error('Connection error', err.stack));
|
.catch(err => console.error('Connection error', err.stack));
|
||||||
|
|
||||||
// API to save turbines
|
// API to save turbines
|
||||||
|
|
@ -32,13 +36,9 @@ app.post('/api/wea', async (req, res) => {
|
||||||
await client.query('BEGIN');
|
await client.query('BEGIN');
|
||||||
|
|
||||||
// Delete existing turbines for this project before saving new state
|
// Delete existing turbines for this project before saving new state
|
||||||
// (This ensures we always have the latest state on 'Save')
|
|
||||||
await client.query('DELETE FROM geodaten.wea_standorte WHERE projekt_id = $1', [projekt_id]);
|
await client.query('DELETE FROM geodaten.wea_standorte WHERE projekt_id = $1', [projekt_id]);
|
||||||
|
|
||||||
for(let t of turbines) {
|
for(let t of turbines) {
|
||||||
// Mapping frontend data to geodaten.wea_standorte columns:
|
|
||||||
// wea_nummer, hersteller, anlagentyp, nabenhoehe, rotordurchmesser, ksf_drehung, projekt_id, geom
|
|
||||||
|
|
||||||
await client.query(
|
await client.query(
|
||||||
`INSERT INTO geodaten.wea_standorte (
|
`INSERT INTO geodaten.wea_standorte (
|
||||||
wea_nummer, hersteller, anlagentyp, nabenhoehe, rotordurchmesser, ksf_drehung, projekt_id, geom
|
wea_nummer, hersteller, anlagentyp, nabenhoehe, rotordurchmesser, ksf_drehung, projekt_id, geom
|
||||||
|
|
@ -85,6 +85,11 @@ app.get('/api/wea/:projekt_id', async (req, res) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(port, () => {
|
// Catch-all route to serve index.html for any other request (optional, good for SPAs)
|
||||||
console.log(`Backend server listening at http://localhost:${port}`);
|
app.get('*', (req, res) => {
|
||||||
|
res.sendFile(path.join(__dirname, 'index.html'));
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(port, '0.0.0.0', () => {
|
||||||
|
console.log(`Server running at http://0.0.0.0:${port}`);
|
||||||
});
|
});
|
||||||
Loading…
Reference in New Issue