Сделал альфа версию веб-сервера и интеграцию с базой данных

This commit is contained in:
12 changed files with 3112 additions and 51 deletions

29
server.js Normal file
View File

@@ -0,0 +1,29 @@
import express from 'express'
import Database from 'sqlite3'
import { fileURLToPath } from 'url'
import { dirname, join } from 'path'
const __dirname = dirname(fileURLToPath(import.meta.url))
const app = express()
const PORT = 3000
const db = new Database.Database(join(__dirname, 'data.db'))
app.use(express.static(join(__dirname, 'dist')))
// получение списка регионов из БД
app.get('/api/regions', (_req, res) => {
db.all('SELECT id, region_name FROM regions ORDER BY id', (err, rows) => {
if (err) return res.status(500).json({ error: 'Ошибка получения регионов' })
res.json(rows)
})
})
// и что им не понравилось в *?
app.get('/{*splat}', (_req, res) => {
res.sendFile(join(__dirname, 'dist', 'index.html'))
})
app.listen(PORT, () => {
console.log(`Сервер запущен: http://localhost:${PORT}`)
})