Skip to main content
Module

x/fastro/examples/basic.ts

Fast and simple web application framework for deno
Go to Latest
File
import { Fastro } from "../mod.ts";
// you must set payload true to get the payloadconst server = new Fastro({ payload: true });
server // very basic usage of `get` .get("/", (req) => req.send("root")) // get url parameters from `get` method // for example, you define a route method with url `/hi` // you can access url with dynamic url: // http://localhost:3000/hi/world/how/are/you .get("/hi", (req) => { const params = req.parameter; // result is an array ["hi","world","how","are","you"] req.send(params); }) // get a payload from `post` method .post("/", (req) => { const payload = req.payload; req.send(`Payload: ${payload}`); }) // get a payload from `put` method .put("/", (req) => { const payload = req.payload; req.send(`Payload: ${payload}`); }) // very basic usage of `delete` .delete("/", (req) => req.send("delete"));
server // send json response .get("/json", (req) => { const json = { name: "john", active: true, }; req.send(json); }) // send array response .get("/array", (req) => { const array: number[] = [1, 2, 3]; req.send(array); }) // send content of text file response .get("/file", async (req) => { const file = await Deno.readFile("mod.ts"); const httpStatus = 200; const headers = new Headers(); headers.set("content-type", "text/plain; charset=utf-8"); req.send(file, httpStatus, headers); });
await server.listen({ port: 3000 });