Добавить server.js
This commit is contained in:
parent
7811bef5f3
commit
8a547c8437
78
server.js
Normal file
78
server.js
Normal file
@ -0,0 +1,78 @@
|
||||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
const sqlite3 = require('sqlite3').verbose();
|
||||
const cors = require('cors');
|
||||
|
||||
const app = express();
|
||||
const PORT = 3000;
|
||||
|
||||
const db = new sqlite3.Database('./database.db');
|
||||
|
||||
db.serialize(() => {
|
||||
db.run(`CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
email TEXT UNIQUE,
|
||||
password TEXT
|
||||
)`);
|
||||
|
||||
db.run(`CREATE TABLE IF NOT EXISTS services (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
userId INTEGER,
|
||||
type TEXT,
|
||||
target TEXT,
|
||||
paid INTEGER DEFAULT 0,
|
||||
FOREIGN KEY(userId) REFERENCES users(id)
|
||||
)`);
|
||||
});
|
||||
|
||||
app.use(cors({
|
||||
origin: 'https://ngcd.center',
|
||||
methods: ['GET', 'POST', 'OPTIONS'],
|
||||
allowedHeaders: ['Content-Type']
|
||||
}));
|
||||
|
||||
app.use(bodyParser.json());
|
||||
|
||||
app.post('/api/register', (req, res) => {
|
||||
const { email, password } = req.body;
|
||||
db.run('INSERT INTO users (email, password) VALUES (?, ?)', [email, password], function (err) {
|
||||
if (err) return res.status(400).json({ error: 'Пользователь уже существует' });
|
||||
res.json({ success: true, userId: this.lastID });
|
||||
});
|
||||
});
|
||||
|
||||
app.post('/api/login', (req, res) => {
|
||||
const { email, password } = req.body;
|
||||
db.get('SELECT * FROM users WHERE email = ? AND password = ?', [email, password], (err, row) => {
|
||||
if (row) res.json({ success: true, userId: row.id });
|
||||
else res.status(400).json({ error: 'Неверный email или пароль' });
|
||||
});
|
||||
});
|
||||
|
||||
app.post('/api/add-service', (req, res) => {
|
||||
const { userId, type, target } = req.body;
|
||||
db.run('INSERT INTO services (userId, type, target, paid) VALUES (?, ?, ?, 0)', [userId, type, target], function (err) {
|
||||
if (err) return res.status(500).json({ error: 'Ошибка при добавлении' });
|
||||
res.json({ success: true, serviceId: this.lastID });
|
||||
});
|
||||
});
|
||||
|
||||
app.post('/api/pay', (req, res) => {
|
||||
const { userId } = req.body;
|
||||
db.run('UPDATE services SET paid = 1 WHERE userId = ? AND paid = 0', [userId], function (err) {
|
||||
if (err) return res.status(500).json({ error: 'Ошибка при оплате' });
|
||||
res.json({ success: true });
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/api/services/:userId', (req, res) => {
|
||||
const userId = req.params.userId;
|
||||
db.all('SELECT * FROM services WHERE userId = ? AND paid = 1', [userId], (err, rows) => {
|
||||
if (err) return res.status(500).json({ error: 'Ошибка при получении услуг' });
|
||||
res.json(rows);
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`✅ Server running at http://localhost:${PORT}`);
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user