oleg-name-server 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cli.js +14 -0
- package/db.json +6 -0
- package/package.json +20 -0
- package/src/db.js +10 -0
- package/src/server.js +72 -0
package/bin/cli.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import path from "path"
|
|
3
|
+
import { startServer } from "../src/server.js"
|
|
4
|
+
|
|
5
|
+
const args = process.argv.slice(2)
|
|
6
|
+
const portIndex = args.indexOf("--port")
|
|
7
|
+
const fileIndex = args.indexOf("--file")
|
|
8
|
+
|
|
9
|
+
const port = portIndex >= 0 ? Number(args[portIndex + 1]) : 3000
|
|
10
|
+
const file = fileIndex >= 0 ? args[fileIndex + 1] : "db.json"
|
|
11
|
+
|
|
12
|
+
const filePath = path.resolve(process.cwd(), file)
|
|
13
|
+
|
|
14
|
+
startServer({ port, filePath })
|
package/db.json
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "oleg-name-server",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [],
|
|
11
|
+
"bin": {
|
|
12
|
+
"mini-json-server": "bin/cli.js"
|
|
13
|
+
},
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"cors": "^2.8.6",
|
|
18
|
+
"express": "^5.2.1"
|
|
19
|
+
}
|
|
20
|
+
}
|
package/src/db.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import fs from "fs/promises"
|
|
2
|
+
|
|
3
|
+
export async function readDb(filePath) {
|
|
4
|
+
const raw = await fs.readFile(filePath, "utf8")
|
|
5
|
+
return JSON.parse(raw)
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export async function writeDb(filePath, db) {
|
|
9
|
+
await fs.writeFile(filePath, JSON.stringify(db, null, 2))
|
|
10
|
+
}
|
package/src/server.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import express from "express"
|
|
2
|
+
import cors from "cors"
|
|
3
|
+
import crypto from "crypto"
|
|
4
|
+
import { readDb, writeDb } from "./db.js"
|
|
5
|
+
|
|
6
|
+
export function startServer({ port, filePath }) {
|
|
7
|
+
const app = express()
|
|
8
|
+
app.use(cors())
|
|
9
|
+
app.use(express.json())
|
|
10
|
+
|
|
11
|
+
app.get("/health", (req, res) => res.json({ ok: true }))
|
|
12
|
+
|
|
13
|
+
app.get("/:collection", async (req, res) => {
|
|
14
|
+
const db = await readDb(filePath)
|
|
15
|
+
res.json(db[req.params.collection] || [])
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
app.get("/:collection/:id", async (req, res) => {
|
|
19
|
+
const db = await readDb(filePath)
|
|
20
|
+
const list = db[req.params.collection] || []
|
|
21
|
+
const item = list.find(x => String(x.id) === req.params.id)
|
|
22
|
+
if (!item) return res.status(404).json({ error: "Not found" })
|
|
23
|
+
res.json(item)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
app.post("/:collection", async (req, res) => {
|
|
27
|
+
const db = await readDb(filePath)
|
|
28
|
+
const list = db[req.params.collection] || []
|
|
29
|
+
const item = { id: crypto.randomUUID(), ...req.body }
|
|
30
|
+
list.push(item)
|
|
31
|
+
db[req.params.collection] = list
|
|
32
|
+
await writeDb(filePath, db)
|
|
33
|
+
res.status(201).json(item)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
app.put("/:collection/:id", async (req, res) => {
|
|
37
|
+
const db = await readDb(filePath)
|
|
38
|
+
const list = db[req.params.collection] || []
|
|
39
|
+
const idx = list.findIndex(x => String(x.id) === req.params.id)
|
|
40
|
+
if (idx === -1) return res.status(404).json({ error: "Not found" })
|
|
41
|
+
list[idx] = { id: req.params.id, ...req.body }
|
|
42
|
+
db[req.params.collection] = list
|
|
43
|
+
await writeDb(filePath, db)
|
|
44
|
+
res.json(list[idx])
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
app.patch("/:collection/:id", async (req, res) => {
|
|
48
|
+
const db = await readDb(filePath)
|
|
49
|
+
const list = db[req.params.collection] || []
|
|
50
|
+
const idx = list.findIndex(x => String(x.id) === req.params.id)
|
|
51
|
+
if (idx === -1) return res.status(404).json({ error: "Not found" })
|
|
52
|
+
list[idx] = { ...list[idx], ...req.body }
|
|
53
|
+
db[req.params.collection] = list
|
|
54
|
+
await writeDb(filePath, db)
|
|
55
|
+
res.json(list[idx])
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
app.delete("/:collection/:id", async (req, res) => {
|
|
59
|
+
const db = await readDb(filePath)
|
|
60
|
+
const list = db[req.params.collection] || []
|
|
61
|
+
const idx = list.findIndex(x => String(x.id) === req.params.id)
|
|
62
|
+
if (idx === -1) return res.status(404).json({ error: "Not found" })
|
|
63
|
+
const [removed] = list.splice(idx, 1)
|
|
64
|
+
db[req.params.collection] = list
|
|
65
|
+
await writeDb(filePath, db)
|
|
66
|
+
res.json(removed)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
app.listen(port, () => {
|
|
70
|
+
console.log(`Server running on http://localhost:${port}`)
|
|
71
|
+
})
|
|
72
|
+
}
|