Skip to main content

App

For create an application in Alosaur, you must use the class App.

// HomeArea
// Create alosaur applicationconst app = new App({  areas: [HomeArea],});
// Listen :8000 port by defaultapp.listen();

The application can be plisted with TState, TState need to typing the status of the context of the request.

// App<TState> // example uses in Controller action
@Get()@ActionParam(0, Context())getBody(context: HttpContext<TState>): TState {  return context.state;}
important

You can read more about Context in - Context, Request, Response

AppSettings#

The App constructor takes the AppSettings interface

areas

Areas of controllers

middlewares

Middleware that registers in the entire application

staticConfig

Configure to output static files from a certain directory by a certain route

{  root: `${Deno.cwd()}/examples/static/www`,  index: "index.html",  baseRoute: "/www/", // or undefined for default route /} 
// or use methodconst app = new App();
app.useStatic({...})

viewRenderConfig

Configuration to use the default renderer in method outputs, via a function such as return View('index')

// Create Handlebars configconst handle = new Handlebars({  baseDir: viewPath,  extname: ".hbs",  layoutsDir: "layouts/",  partialsDir: "partials/",  defaultLayout: "main",  helpers: undefined,  compilerOptions: undefined,});
app.useViewRender({  type: "handlebars",  basePath: `${Deno.cwd()}/examples/handlebars/views`,  getBody: async (path: string, model: any, config: ViewRenderConfig) =>    await handle.renderView(path, model),});
important

Read more about the outputs of the controller methods in the section: Controllers

logging

Output to the console service information on the registration of controllers and routes to them

container

Provides default base container implementation in the DI

providers

Providers declared at the root level

important

Подробнее про providers в разделе: Dependency injection

App.listen()#

With this function you start the server to listen for http requests. By default, the server runs on port :8000.

You can also run the server by selecting the one you want:

app.listen("http://localhost:8080") 
// or app.listen(":8080")
// or with custom listener
app.listen(null, Deno.listen({ port: 8080 }))

Global error handler#

Errors that haven't been caught elsewhere get in here

const app = new App(  // app settings);
// added global error handlerapp.error((context: Context<any>, error: Error) => {  context.response.result = Content(    "This page unprocessed error",    (error as HttpError).httpCode || 500,  );  context.response.setImmediately();});