# Create your first controller

Create a new CalendarCtrl.ts in your controllers directory (by default root/controllers). All controllers declared with Controller decorators are considered as Platform routers (Express.Router, Koa.Router, ...). A Platform router requires a path (here, the path is /calendars) to expose an url on your server. More precisely, it's a part of a path, and the entire exposed url depends on the Server configuration (see Configuration) and the children controllers.

In this case, we have no dependencies and the root endpoint is set to /rest. So the controller's url will be http://host/rest/calendars.

import {Get} from "@tsed/schema";
import {Controller} from "@tsed/di";

@Controller("/calendars")
export class CalendarCtrl {
  @Get()
  findAll(): string {
    return "This action returns all calendars";
  }
}
1
2
3
4
5
6
7
8
9
10

TIP

Decorators Get , Post , Delete , Put , etc., support dynamic pathParams (eg: /:id) and RegExp like Express API.

See Controllers section for more details

WARNING

You have to configure engine rendering if you want to use the decorator Render .

The CalendarCtrl you just added needs to be connected by exporting it. Find a index.ts in my case in the /rest/ folder and add the following line:

export * from "./CalendarCtrl";
1

To test your method, just run your server.ts and send an HTTP request on /rest/calendars/.

# Ready for More?

We’ve briefly introduced the most basic features of Ts.ED - the rest of this guide will cover them and other advanced features with much finer details, so make sure to read through it all!

Last Updated: 4/13/2024, 10:47:55 AM

Other topics