diff options
author | rohan09-raj <rajrohan1914@gmail.com> | 2024-02-19 20:50:55 +0530 |
---|---|---|
committer | Blaster4385 <blaster4385@tablaster.dev> | 2024-02-21 23:52:45 +0530 |
commit | 23de613393d2399babe3a7b4d2fb77d772fcfa87 (patch) | |
tree | 8cd76f758fd4a4e0442c45badbe41b303f5155b8 | |
parent | 7ef0354687bb6abeda97a32ebcb0e39de91691e5 (diff) |
feat: implement backend in node.js
- Use sqlite as a database
-rwxr-xr-x | server/bun.lockb | bin | 3124 -> 79826 bytes | |||
-rw-r--r-- | server/controllers/bin.js | 100 | ||||
-rw-r--r-- | server/controllers/health.js | 7 | ||||
-rw-r--r-- | server/index.js | 18 | ||||
-rw-r--r-- | server/index.ts | 1 | ||||
-rw-r--r-- | server/package.json | 22 | ||||
-rw-r--r-- | server/routes/bin.js | 11 | ||||
-rw-r--r-- | server/routes/health.js | 8 |
8 files changed, 161 insertions, 6 deletions
diff --git a/server/bun.lockb b/server/bun.lockb Binary files differindex ba8697f..0ba4f0e 100755 --- a/server/bun.lockb +++ b/server/bun.lockb diff --git a/server/controllers/bin.js b/server/controllers/bin.js new file mode 100644 index 0000000..882ecef --- /dev/null +++ b/server/controllers/bin.js @@ -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 }; diff --git a/server/controllers/health.js b/server/controllers/health.js new file mode 100644 index 0000000..d31c489 --- /dev/null +++ b/server/controllers/health.js @@ -0,0 +1,7 @@ +const healthCheck = async (req, res) => { + return res.json({ + uptime: process.uptime(), + }); +}; + +export default { healthCheck }; diff --git a/server/index.js b/server/index.js new file mode 100644 index 0000000..57546fd --- /dev/null +++ b/server/index.js @@ -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}`)); diff --git a/server/index.ts b/server/index.ts deleted file mode 100644 index f67b2c6..0000000 --- a/server/index.ts +++ /dev/null @@ -1 +0,0 @@ -console.log("Hello via Bun!");
\ No newline at end of file diff --git a/server/package.json b/server/package.json index ef41d08..a74b362 100644 --- a/server/package.json +++ b/server/package.json @@ -1,11 +1,23 @@ { - "name": "minibun", - "module": "index.ts", + "name": "minibun-server", + "version": "1.0.0", + "description": "", + "main": "index.js", "type": "module", + "scripts": { + "start": "nodemon index.js", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", "devDependencies": { - "@types/bun": "latest" + "cors": "^2.8.5", + "dotenv": "^16.4.4", + "nodemon": "^3.0.3" }, - "peerDependencies": { - "typescript": "^5.0.0" + "dependencies": { + "express": "^4.18.2", + "sqlite3": "^5.1.7" } }
\ No newline at end of file diff --git a/server/routes/bin.js b/server/routes/bin.js new file mode 100644 index 0000000..38b2ddc --- /dev/null +++ b/server/routes/bin.js @@ -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; diff --git a/server/routes/health.js b/server/routes/health.js new file mode 100644 index 0000000..04275c9 --- /dev/null +++ b/server/routes/health.js @@ -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; |