feat: implement backend in node.js
- Use sqlite as a database
This commit is contained in:
parent
7ef0354687
commit
23de613393
8 changed files with 162 additions and 7 deletions
BIN
server/bun.lockb
BIN
server/bun.lockb
Binary file not shown.
100
server/controllers/bin.js
Normal file
100
server/controllers/bin.js
Normal file
|
@ -0,0 +1,100 @@
|
|||
import sqlite3 from "sqlite3";
|
||||
|
||||
const dbPath = "./database/bins.db";
|
||||
|
||||
function createBinTable() {
|
||||
const db = new sqlite3.Database(dbPath, (err) => {
|
||||
if (err) {
|
||||
console.error("Error connecting to database:", err.message);
|
||||
return;
|
||||
}
|
||||
|
||||
const sql = `CREATE TABLE IF NOT EXISTS bin (
|
||||
id TEXT PRIMARY KEY,
|
||||
html_content TEXT
|
||||
)`;
|
||||
|
||||
db.run(sql, (err) => {
|
||||
if (err) {
|
||||
console.error("Error creating table:", err.message);
|
||||
} else {
|
||||
console.log("`bin` table created successfully");
|
||||
}
|
||||
|
||||
db.close((err) => {
|
||||
if (err) {
|
||||
console.error("Error closing database:", err.message);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const createBin = (req, res) => {
|
||||
console.log(`createBin: ${req.body}`);
|
||||
const { html_content } = req.body;
|
||||
const id = Math.random().toString(36).substring(7);
|
||||
|
||||
const db = new sqlite3.Database(dbPath, (err) => {
|
||||
if (err) {
|
||||
console.error("Error connecting to database:", err.message);
|
||||
throw err; // Re-throw for async handling
|
||||
}
|
||||
|
||||
const sql = `INSERT INTO bin (id, html_content) VALUES (?, ?)`;
|
||||
|
||||
db.run(sql, [id, html_content], (err) => {
|
||||
if (err) {
|
||||
console.error("Error creating entry:", err.message);
|
||||
throw err; // Re-throw for async handling
|
||||
} else {
|
||||
res.status(201).json({ id });
|
||||
console.log("Entry created successfully:", id);
|
||||
}
|
||||
|
||||
db.close((err) => {
|
||||
if (err) {
|
||||
console.error("Error closing database:", err.message);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const getBin = async (req, res) => {
|
||||
const { id } = req.params;
|
||||
if (!id) {
|
||||
throw new Error("Missing required parameter: id"); // Throw error for missing ID
|
||||
}
|
||||
|
||||
const db = new sqlite3.Database(dbPath, (err) => {
|
||||
if (err) {
|
||||
console.error("Error connecting to database:", err.message);
|
||||
throw err; // Re-throw for async handling
|
||||
}
|
||||
|
||||
const sql = `SELECT * FROM bin WHERE id = ?`;
|
||||
|
||||
db.get(sql, [id], (err, row) => {
|
||||
if (err) {
|
||||
console.error("Error retrieving entry:", err.message);
|
||||
throw err; // Re-throw for async handling
|
||||
} else if (!row) {
|
||||
console.log("Entry not found:", id);
|
||||
return null; // Handle case where no entry exists
|
||||
}
|
||||
|
||||
res.status(200).json(row);
|
||||
console.log("Entry retrieved:", id);
|
||||
db.close((err) => {
|
||||
if (err) {
|
||||
console.error("Error closing database:", err.message);
|
||||
}
|
||||
});
|
||||
|
||||
return row;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export default { createBinTable, createBin, getBin };
|
7
server/controllers/health.js
Normal file
7
server/controllers/health.js
Normal file
|
@ -0,0 +1,7 @@
|
|||
const healthCheck = async (req, res) => {
|
||||
return res.json({
|
||||
uptime: process.uptime(),
|
||||
});
|
||||
};
|
||||
|
||||
export default { healthCheck };
|
18
server/index.js
Normal file
18
server/index.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
import Express from "express";
|
||||
import cors from "cors";
|
||||
import dotenv from "dotenv";
|
||||
|
||||
import binRoutes from "./routes/bin.js";
|
||||
import healthRoutes from "./routes/health.js";
|
||||
|
||||
const app = Express();
|
||||
dotenv.config();
|
||||
|
||||
app.use(cors());
|
||||
app.use(Express.json());
|
||||
app.use("/", healthRoutes);
|
||||
app.use("/bin", binRoutes);
|
||||
|
||||
const PORT = process.env.PORT;
|
||||
|
||||
app.listen(PORT, () => console.log(`Server running on PORT: ${PORT}`));
|
|
@ -1 +0,0 @@
|
|||
console.log("Hello via Bun!");
|
|
@ -1,11 +1,23 @@
|
|||
{
|
||||
"name": "minibun",
|
||||
"module": "index.ts",
|
||||
"name": "minibun-server",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"devDependencies": {
|
||||
"@types/bun": "latest"
|
||||
"scripts": {
|
||||
"start": "nodemon index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"typescript": "^5.0.0"
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.4.4",
|
||||
"nodemon": "^3.0.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "^4.18.2",
|
||||
"sqlite3": "^5.1.7"
|
||||
}
|
||||
}
|
11
server/routes/bin.js
Normal file
11
server/routes/bin.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
import Express from "express";
|
||||
import bin from "../controllers/bin.js";
|
||||
|
||||
const router = Express.Router();
|
||||
|
||||
bin.createBinTable();
|
||||
|
||||
router.get("/:id", bin.getBin);
|
||||
router.post("/", bin.createBin);
|
||||
|
||||
export default router;
|
8
server/routes/health.js
Normal file
8
server/routes/health.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
import Express from "express";
|
||||
import health from "../controllers/health.js";
|
||||
|
||||
const router = Express.Router();
|
||||
|
||||
router.get("/health", health.healthCheck);
|
||||
|
||||
export default router;
|
Loading…
Reference in a new issue