Ich entwickle gerade einen einfachen Node.js-Server mit Express, CORS und MySQL. Zusätzlich werden statische Dateien (wie z. B. eine index.html
) aus dem public
-Ordner ausgeliefert.
Hier ist mein aktueller Code:
`
js
const express = require('express');
const cors = require('cors');
const path = require('path');
const mysql = require('mysql');
const app = express();
app.use(cors());
// MySQL-Verbindung
const con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "forum"
});
// Statische Dateien ausliefern
app.use(express.static(path.join(__dirname, 'public')));
// Test-Route
app.get('/test-connection', (req, res) => {
con.connect(function(err) {
if (err) {
res.send('Connection failed: ' + err.message);
} else {
res.send('Connected!');
}
});
});
app.listen(3000, () => {
console.log('Server läuft auf Port 3000');
});