Сделал альфа версию веб-сервера и интеграцию с базой данных
This commit is contained in:
29
server.js
Normal file
29
server.js
Normal 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}`)
|
||||
})
|
||||
Reference in New Issue
Block a user