fastro

deno fast web framework

https://github.com/fastrojs/fastro-server

.github

benchmarks

cli

core

examples

middlewares

plugins

test

.gitignore

CNAME

README.md

_config.yml

deps.ts

mod.ts

fastro

ci

Fastro is web framework for developers who are obsessed with simplicity & performance.

It is inspired by Express, Fastify, Nest & Firebase.

import { Fastro } from "https://raw.githubusercontent.com/fastrodev/fastro/v0.12.2/mod.ts";
const server = new Fastro();
server.get("/", (req) => req.send("root"));
await server.listen();

Table of contents

Benchmarks

If performance is really important to you, here are the Hello World benchmark results. Check this folder to see the details.

FrameworkVersionRequests/sRouter?
Deno http1.1.216321.2โœ—
Node http14.3.016013.2โœ—
Fastro0.12.213782.4โœ“
Fastify2.15.012157.8โœ“
Abc1.0.0-rc1011628.6โœ“
Oak5.2.010525.8โœ“
Express4.17.16933.3โœ“
PHP7.3.115390.5โœ—
Python Flask1.1.2586.3โœ“

How to use

This module uses the git release. If you want to pick a specific version, for example 0.12.2, then the full url is:

https://raw.githubusercontent.com/fastrodev/fastro/v0.12.2/mod.ts

If you do not use the version, it will refer to master branch. Breaking changes may be made without warning.

Middleware

You can add new properties or functions for specific URL to the default request. This is similar to the express middleware.

const middleware = (req: Request, done: Function) => {
  req.oke = () => req.send("oke");
  done();
};

server
  .use(middleware)
  .get("/", (req) => req.oke());

Decorator

Another way to add a new property or function globally to the fastro instance and request object is to use a decorator. This is similar to the fastify decorator.

server
  .decorate((instance) => instance.ok = "ok")
  .decorate((instance) => instance.hello = (payload: string) => payload)
  .decorateRequest((req) => req.oke = "oke request");

server
  .get("/", (req) => req.send(server.ok))
  .get("/hello", (req) => req.send(server.hello("hello")))
  .get("/oke", (req) => req.send(req.oke));

Plugin

You can add new properties or functions to the fastro instance. You can also use all default instance functions, include decorator, create routes & middleware. This is similar to the fastify plugin.

const routes = function (fastro: Fastro, done: Function) {
  fastro
    .get("/", (req) => req.send("root"))
    .post("/", (req) => req.send("post"))
    .put("/", (req) => req.send("put"))
    .delete("/", (req) => req.send("delete"));
  done();
};

server.register(routes);

Dependency Injection

With dependency injection you can create complex applications with clean code. No longer need to manually import handlers and services. You only make a class and add typescript decorator to define gateway, controller, service and route. Fastro will automatically load, register and create them for you. This is similar to nest.

import { Controller, Get, Request } from "https://raw.githubusercontent.com/fastrodev/fastro/v0.12.2/mod.ts";

@Controller()
class Greet {

  @Get()
  handler(req: Request) {
    req.send("root");
  }
}

Function

With functions, you only need to define the main url and the handler. There is no need to define a method, so you can use all types of http methods. You can also get the url parameters more dynamically without defining the full url.

server.function("/prefix/function", (req) => {
  if (!req.url.includes("/prefix/function")) return server.forward(req);
  req.send(req.functionParameter);
});

Command line interface

fastro-cli is only used for fastro-function.

Install fastro-cli:

deno install -f --allow-net --allow-write --allow-read https://raw.githubusercontent.com/fastrodev/fastro/v0.12.2/cli/fastro.ts

Create config file and initial handler:

mkdir app && cd app && fastro init

Run server:

fastro serve

You can access app root via url:

http://localhost:3000/app

You can access the handler via url:

http://localhost:3000/app/hello

You can change the default app and handler by updating the file generated from fastro init. You can create multiple handlers in one app.

Cloud Function

You can try our free fastro cloud function via command line.

Register your email:

fastro register --email your@email.com

Deploy your code:

fastro deploy

Then, you can access your handler via url:

https://api.fastro.dev/<YOUR_APP>/<YOUR_HANDLER>

Example URL:

https://api.fastro.dev/app/hello

Examples

Check this folder to find out how to: