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
|
|
@ -10,4 +10,4 @@ COPY . .
|
|||
|
||||
# Der Server läuft auf Port 3000
|
||||
EXPOSE 3000
|
||||
CMD ["node", "server.js"]
|
||||
CMD ["node", "server.js"]
|
||||
|
|
|
|||
2
app.js
2
app.js
|
|
@ -1434,7 +1434,7 @@ document.addEventListener('DOMContentLoaded', async () => {
|
|||
}));
|
||||
|
||||
try {
|
||||
const response = await fetch('http://localhost:3000/api/wea', {
|
||||
const response = await fetch('/api/wea', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
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 bodyParser = require('body-parser');
|
||||
const cors = require('cors');
|
||||
const path = require('path');
|
||||
|
||||
const app = express();
|
||||
const port = process.env.PORT || 3000;
|
||||
|
|
@ -10,6 +11,9 @@ const port = process.env.PORT || 3000;
|
|||
app.use(cors());
|
||||
app.use(bodyParser.json());
|
||||
|
||||
// Serve static files from the root directory
|
||||
app.use(express.static(path.join(__dirname, './')));
|
||||
|
||||
const pool = new Pool({
|
||||
host: process.env.DB_HOST,
|
||||
port: process.env.DB_PORT,
|
||||
|
|
@ -20,7 +24,7 @@ const pool = new Pool({
|
|||
|
||||
// Test DB Connection
|
||||
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));
|
||||
|
||||
// API to save turbines
|
||||
|
|
@ -32,13 +36,9 @@ app.post('/api/wea', async (req, res) => {
|
|||
await client.query('BEGIN');
|
||||
|
||||
// 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]);
|
||||
|
||||
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(
|
||||
`INSERT INTO geodaten.wea_standorte (
|
||||
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, () => {
|
||||
console.log(`Backend server listening at http://localhost:${port}`);
|
||||
// Catch-all route to serve index.html for any other request (optional, good for SPAs)
|
||||
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