Skip to main content
Module

x/fastro/examples/use_plugin.ts

Fast and simple web application framework for deno
Go to Latest
File
import { Fastro, FastroRequest } from "../mod.ts";
const server = new Fastro();
// compare parameter with local variablefunction parameterPlugin(req: FastroRequest) { const data = "hello"; if (req.parameter.hello === data) { console.log(req.parameter); }}
// get client headers & custom send methodfunction sendOk(req: FastroRequest) { console.log(req.headers.get("host")); req.sendOk = (payload: string) => { const headers = new Headers(); headers.set("X-token", "your_token"); return req.send(payload, 200, headers); };}
// add new function & propertyfunction payloadPlugin(req: FastroRequest) { req.newProp = new Date(); req.ok = function (param: string) { console.log("param inside plugin:", param); };}
// add plugins to serverserver .use(payloadPlugin) .use(parameterPlugin) .use(sendOk);
server .get("/:hello", (req) => req.send("hello")) .post("/:hello", (req) => { // access new property console.log("new property:", req.newProp); // access new function req.ok("hello"); // access custom send function req.sendOk("ok deh"); });
await server.listen({ port: 8000 });