Production Node.js + Express + SQLite Backend (`server.js`)
Run this on your VPS (`node server.js`) to store cards locally in an SQLite database with CORS and RESTful routes.
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const cors = require('cors');
const app = express();
app.use(express.json());
app.use(cors());
// Initialize SQLite database locally on VPS
const db = new sqlite3.Database('./ionest_cards.db');
db.serialize(() => {
db.run(`CREATE TABLE IF NOT EXISTS cards (
id TEXT PRIMARY KEY,
userId TEXT,
fullName TEXT,
designation TEXT,
company TEXT,
category TEXT,
phone TEXT,
email TEXT,
website TEXT,
address TEXT,
dealValue REAL,
notes TEXT,
createdAt TEXT
)`);
});
// GET all business cards
app.get('/api/cards', (req, res) => {
db.all("SELECT * FROM cards", [], (err, rows) => {
if (err) return res.status(500).json({ error: err.message });
res.json(rows);
});
});
// POST / PUT business card
app.post('/api/cards', (req, res) => {
const c = req.body;
const stmt = db.prepare(`INSERT OR REPLACE INTO cards VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
stmt.run(c.id, c.userId, c.fullName, c.designation, c.company, c.category, c.phone, c.email, c.website, c.address, c.dealValue, c.notes, c.createdAt, (err) => {
if (err) return res.status(500).json({ error: err.message });
res.json({ status: "success", card: c });
});
});
// DELETE business card
app.delete('/api/cards/:id', (req, res) => {
db.run(`DELETE FROM cards WHERE id = ?`, req.params.id, (err) => {
if (err) return res.status(500).json({ error: err.message });
res.json({ status: "deleted" });
});
});
app.listen(5000, () => console.log('io.Nest Cards API listening on port 5000'));