Код: Выделить всё
const express = require('express');
const cors = require('cors');
const path = require('path');
const mysql = require('mysql');
const app = express();
app.use(cors());
// MySQL connection
const con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "forum"
});
// Serve static files
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 is running on port 3000');
});
//
< /code>
У меня есть два основных вопроса: < /p>
Is this structure reasonable for a simple backend?
Are there best practices I'm missing (especially regarding connection handling, CORS, or static file serving)?
Подробнее здесь: https://stackoverflow.com/questions/796 ... -api-setup