Skip to main content

Açai Server Module

Build Status Support

The server is responsible for actually handling incoming requests, and delivering them back to the user. It has support for middlewares and providers.

Usage

import server from "https://deno.land/x/acai_server/mod.ts";

// we are going to use Açai's router to control our routes, but you can easily overwrite this, will be shown next
const route = server.getRoute();

route.options({middleware: ["test"]}, () => {
    route.get("/", "file/test@index");
});

const instance = new server();

// Middlewares are optional
instance.addMiddleware("test", (data, next) => next(data));

instance.run();

Overwriting router

If you do not wish to use our router, you can easily overwrite it:

const instance = new server();

instance.setOnRequest((url: string, method: string) => {
    return {
        route	: url,
        file	: "..",
        // method to call inside of the file (optional)
        method	: "..",
        // extra options, such as middleware, etc
        options : {},
        // query params
        params	: {},
    };
});

You can use this for example, if you wish to point all routes to a single frontend, in case of a SPA.

Middlewares

Middleware is a pipeline that runs before the request actually reaches your code, making changes to the request content, or even bailing it out. You can use them for authentication, validation or anything alike.

const instance = new server();

// aliased middleware
instance.addMiddleware("test", (data, next) => next(data));

// grouped aliased middlewares
instance.addMiddlewares({
    test: (data, next) => next(data),
    auth: (data, next) => "404",
});

// global middleware
instance.addGlobalMiddleware([
    (data, next) => next(data),
]);

instance.run();
  • global middlewares will run in every request of your application
  • aliased middleware are middlewares that you can call through your routes, for privileged requests, and such.