Proyecto inicial, con fallitos de CSS. (Gracias Karim.)

This commit is contained in:
2026-02-18 03:09:08 +01:00
commit c7b9f87ad2
20 changed files with 9062 additions and 0 deletions

BIN
backend/blog.db Normal file

Binary file not shown.

62
backend/src/index.js Normal file
View File

@@ -0,0 +1,62 @@
import express from 'express';
import cors from 'cors';
import jwt from 'jsonwebtoken';
import bcrypt from 'bcryptjs';
import sqlite3 from 'sqlite3';
import { open } from 'sqlite';
const app = express();
app.use(cors());
app.use(express.json());
const JWT_SECRET = 'josemi_ultra_secret_123';
let db;
(async () => {
db = await open({ filename: './blog.db', driver: sqlite3.Database });
await db.exec(`
CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE, password TEXT);
CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, slug TEXT UNIQUE,
content TEXT, tags TEXT, url TEXT, type TEXT DEFAULT 'POST', created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
`);
const pass = await bcrypt.hash('josemivi', 10);
await db.run('INSERT OR IGNORE INTO users (username, password) VALUES (?, ?)', ['josemi', pass]);
console.log("✅ Backend Conectado");
})();
app.get('/posts', async (req, res) => {
const posts = await db.all('SELECT * FROM posts ORDER BY created_at DESC');
res.json(posts);
});
app.post('/posts/login', async (req, res) => {
const { username, password } = req.body;
const user = await db.get('SELECT * FROM users WHERE username = ?', [username]);
if (user && await bcrypt.compare(password, user.password)) {
const token = jwt.sign({ id: user.id }, JWT_SECRET);
res.json({ token });
} else { res.status(401).json({ error: 'Fallo' }); }
});
app.post('/posts', async (req, res) => {
const token = req.headers.authorization?.split(' ')[1];
if (!token) return res.status(401).json({ error: 'No token' });
const { title, slug, content, tags, url, type } = req.body;
try {
// Obligamos a que el slug no sea undefined
if(!slug) throw new Error("Slug requerido");
await db.run('INSERT INTO posts (title, slug, content, tags, url, type) VALUES (?, ?, ?, ?, ?, ?)', [title, slug, content, tags, url, type]);
res.json({ success: true });
} catch (e) { res.status(400).json({ error: 'Error al guardar' }); }
});
app.delete('/posts/:id', async (req, res) => {
const token = req.headers.authorization?.split(' ')[1];
if (!token) return res.status(401).json({ error: 'No autorizado' });
await db.run('DELETE FROM posts WHERE id = ?', [req.params.id]);
res.json({ success: true });
});
app.listen(9002, () => console.log('🚀 API en 9002'));