Single File Example

If you're going to show code examples or if you're developing a very simple app then you probably wont get much use from having to setup your environment.

Instead you can just create a new instance of the web app, register an endpoint and start listening.

const app = webapp();

app.get('/', () =>
{
    return new StringResponse('Hello world!');
});

app.listen(8080);

console.log('Started');

You can register get, post, put or delete endpoints by using the functions with the same name. They all take the route as the first argument and a handler as the second argument.

The handler's only argument is the request and it is expected to return a response.
You can access the route parameters and the query the same way as in regular controllers, using this.params and this.query. Of course to access the this object you will have to use function () instead of () =>.

app.get('/person/:name', function (request)
{
    return new StringResponse('Hello ' + this.params.name);
});

All services must be manually registered.

app.use(ConfigService);

Since you can not declare dependencies in this mode you will have to manually get them before using them.

const config = this.container.get(ConfigService);

If you want to use the standard controllers you can still do that.

Route({
    route: 'my-page'
})
class MyPageController extends PageController
{
    public page(request): MaybePromise<IRenderable>
    {
        return this.container.create(Renderable, 'my-template-name');
    }
}
app.use(MyPageController);

Previous article

Get Started