Duck 🦆
This Project is inspired by https://github.com/artur-borys/lapis
Example
import { Duck, Router, MiddlewareFunction } from "https://raw.githubusercontent.com/EntenKoeniq/Duck/master/mod.ts";
const PORT = 3000;
const duck = new Duck();
const router = new Router("/api"); // http://localhost:3000/api/...
// Define a middleware
const requestLogger: MiddlewareFunction = (req, res, next) => {
console.log(`Got a request from <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>r</mi><mi>e</mi><mi>q</mi><mi mathvariant="normal">.</mi><mi>r</mi><mi>e</mi><mi>m</mi><mi>o</mi><mi>t</mi><mi>e</mi><mi>A</mi><mi>d</mi><mi>d</mi><mi>r</mi><mi mathvariant="normal">.</mi><mi>h</mi><mi>o</mi><mi>s</mi><mi>t</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mrow><mo>:</mo></mrow><annotation encoding="application/x-tex">{req.remoteAddr.hostname}: </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal">re</span><span class="mord mathnormal" style="margin-right:0.03588em;">q</span><span class="mord">.</span><span class="mord mathnormal">re</span><span class="mord mathnormal">m</span><span class="mord mathnormal">o</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mord mathnormal">A</span><span class="mord mathnormal">dd</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord">.</span><span class="mord mathnormal">h</span><span class="mord mathnormal">os</span><span class="mord mathnormal">t</span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span></span></span></span>{req.method} ${req.url}`);
next();
}
const posts = [{ id: 1, userId: 1, content: "I love ducks 🦆"}];
// use middleware on duck instance (logging before route handling!)
duck.use(requestLogger);
/* ===== GET ===== */
duck.get("/", (req, res) => {
res.file("./index.html");
})
router.get("/posts", (req, res) => { // http://localhost:3000/api/posts
res.send(posts);
});
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 router
duck.use(router);
// use error middleware (at the end! IMPORTANT)
// error middleware has EXACTLY 4 parameters - very important
duck.use(async (error, req, res, next) => {
if (error) return await res.send({ ok: false, error: error.message });
next!();
});
// Finally, listen
duck.listen({ port: PORT }).then(() => {
console.log(`Listening on ${PORT}`);
});