deno.land / x / simpledb@0.3.0 / example.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { Duck } from "https://raw.githubusercontent.com/EntenKoeniq/Duck/master/mod.ts";import { SimpleDB } from "./mod.ts";
const PORT = 80, db = new SimpleDB({filePath: "./test.json"}), duck = new Duck();
duck.get("/insert/:url", async (req, res) => { let result = await db.insert({ url: req.params.url, short: [ ...Array(4) ].map(() => (~~(Math.random() * 36)).toString(36)).join(''), clicks: 0 });
if (result === undefined) return res.status(500).send("ERROR!"); await db.save();
res.send(result);})
duck.get("/delete/:short", async (req, res) => { if (await db.delete({ short: req.params.short })) { await db.save(); return res.send("Deleted!"); }
res.send("Not deleted!");})
duck.get("/:short", async (req, res) => { let result = await db.findOne({ short: req.params.short });
if (result === undefined) return res.status(404).send("not found!");
res.send(`URL: ${result.url}`);})
duck.get("/exists/:url", async (req, res) => { if (await db.exists({ url: req.params.url })) return res.send("found"); res.send("not found");})
duck.get("/replace", async (req, res) => { let result = await db.findOneAndUpdate({ _id: "innha4xa63p9fppyujci1x3c", clicks: 0 }, { url: "google.com" });
if (result === undefined) return res.status(500).send("Not found!");
db.save(); res.send(result);});
// Finally, listenduck.listen({ port: PORT }).then(() => { console.log(`Listening on ${PORT}`);});
simpledb

Version Info

Tagged at
3 years ago