const express = require('express'); const bodyParser = require('body-parser'); const sqlite3 = require('sqlite3').verbose(); const app = express(); const port = 3000; // Middleware app.use(bodyParser.json()); app.use(express.static('public')); // Serve the frontend files // Database setup const db = new sqlite3.Database('blog.db'); db.serialize(() => { db.run("CREATE TABLE IF NOT EXISTS posts (id INTEGER PRIMARY KEY, title TEXT, content TEXT)"); }); // API to create a new post app.post('/api/post', (req, res) => { const { title, content } = req.body; db.run('INSERT INTO posts (title, content) VALUES (?, ?)', [title, content], function(err) { if (err) { return res.status(500).json({ error: 'Database error' }); } res.json({ success: true }); }); }); // API to get all posts app.get('/api/posts', (req, res) => { db.all('SELECT * FROM posts ORDER BY id DESC', [], (err, rows) => { if (err) { return res.status(500).json({ error: 'Database error' }); } res.json(rows); }); }); // Start the server app.listen(port, () => { console.log(`Server running at http://localhost:${port}`); });