🚧 AvleonJs is in active development and not ready for production use.

Usages

How to create a basic avleon application.

There are two ways to design your application: Controller based and Route method based. They can be mixed in the same app.

Using Controller

Controllers group related routes and can take dependencies through the constructor. This is the approach the CLI scaffolds.

app.ts
controllers/welcome.controller.ts
import { Avleon } from '@avleon/core';
import { WelcomeController } from './controllers/welcome.controller';

const app = Avleon.createApplication();
app.useControllers([ WelcomeController ]);

app.run();

See Controller for parameters, REST resources and error handling.

Using Inline Route

For small apps, register handlers directly on the app with mapGet, mapPost, mapPut and mapDelete:

app.ts
import { Avleon } from '@avleon/core';

const app = Avleon.createApplication();

app.mapGet('/', () => 'Welcome to Avleon.');

app.run(); // by default run on :4000

Handlers receive the request and response, and returning an object sends JSON:

app.mapGet('/users/:id', (req) => ({ id: req.params.id }));

app.mapPost('/users', (req) => {
  return { created: req.body };
});

Inline routes return a chainable object for middleware and OpenAPI metadata:

app.mapGet('/secret', handler)
  .useMiddleware([ApiKeyMiddleware])
  .useOpenApi({ tags: ['internal'] });

Returning responses

Returning an object or array sends JSON; returning a string sends it as-is. To control the status code or headers, throw an HttpExceptions error:

import { HttpExceptions } from '@avleon/core';

throw HttpExceptions.notFound('User not found');

Running the app

run takes an optional port, defaulting to 4000:

app.run();      // :4000
app.run(3000);  // :3000

Read the port from the environment to make it configurable:

serve.ts
import { Environment, inject } from '@avleon/core';
import app from './app';

async function serve() {
  const env = inject(Environment);
  await app.run(env.get<number>('PORT') || 4000);
}

serve();

Next steps