deno.land / x / duck_web_framework@0.1.1 / 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
import { Duck, Router } from "./mod.ts";
const PORT = 3000, posts = [{ id: 1, userId: 1, content: "I love ducks 🦆"}], duck = new Duck(), router = new Router("/api"); // http://localhost:3000/api/...
duck.use(Duck.logger);duck.use(Duck.cookies);
/* ===== GET ===== */duck.get("/", (req, res) => { res.file("./index.html");
if (req.cookies?.has("cookieExample")) console.log(`Cookies: ${req.cookies?.get("cookieExample")}`); else req.cookies?.set({ name: "cookieExample", value: "cookieValue"});})
router.get("/posts", (req, res) => { // http://localhost:3000/api/posts res.send(posts);});
router.get("/get/:id", (req, res) => { // http://localhost:3000/api/get/1 let post = posts.find((candidate) => candidate.id === Number(req.params.id));
if (!post) return res.status(404).send("Post not found!");
res.send(post);});
router.get("/error", (req, res, next) => { // http://localhost:3000/api/error next(new Error("SomeError"));});
/* ===== POST ===== */router.post("/push", async (req, res) => { const newPost = { id: posts.length + 1, userId: Number(req.params.id), content: req.body.content }; await posts.push(newPost); res.status(201).send(newPost);});
// use created routerduck.use(router);
// Finally, listenduck.listen({ port: PORT }).then(() => { console.log(`Listening on ${PORT}`);});
duck_web_framework

Version Info

Tagged at
3 years ago