Now it is time to add the controller which will make the REST endpointsavailable to client applications through the REST server.
Scaffold a BASIC controller
Run the following command to create a BASIC controller.
Next, select the Empty Controller.
? What kind of controller would you like to generate? (Use arrow keys)
❯ Empty Controller
REST Controller with CRUD functions
At this point, you should have two files in the src/controllers
directory.
- calculator.controller.ts
- index.tsThe generated Empty controller looks like this. You may remove the comments atthis point.
// Uncomment these imports to begin using these cool features!
export class CalculatorController {
constructor() {}
}
Adding the imports into the Controller
Add the following two lines at the begining of thesrc/controllers/calculator.controller.ts file.
The HttpErrors is used to help use raise the precondition error whenever wefind that the client application is trying to divide by zero.
To complete importing the necessary classes, we need to also include the serviceand the interfaces we modeled in the previous step as follows:
import {
CalculatorParameters,
AddResponse,
MultiplyResponse,
DivideResponse,
SubtractResponse,
} from '../services/calculator.service';
Injecting the Service into the Controller
In the constructor method, we inject the service. Please note that the keyservices.CalculatorService
has not been defined so far, we will inform ourApplication about this key in the last step.
export class CalculatorController {
constructor(
@inject('services.CalculatorService')
protected calculatorService: CalculatorService,
}
Creating the end point methods
Now we need to create an endpoint for each operation. There will be four (4)endpoints in total. Each endpoint is a method inside the CalculatorController
,so make sure the next code segments are placed before the last }
in the file.
Adding the Divide end point
Our divide method looks like this initially. Notice we are using the for type assertion. The Node.js methods defined in theservice are available from here due to the dependency injection availablethrough LB4.
@get('/divide/{intA}/{intB}')
async divide(intA: number, intB: number): Promise<DivideResponse> {
Finally we can use the @param decorator on the two properties to help usvalidate the data type associated to each parameter. In this case we are makingsure the client application only sends integer values as the remote SOAP webservice expects this data type and this is done automatically for us.
The final code looks like as follows:
@get('/divide/{intA}/{intB}')
async divide(
@param.path.integer('intA') intA: number,
@param.path.integer('intB') intB: number,
): Promise<DivideResponse> {
//Preconditions
if (intB === 0) {
throw new HttpErrors.PreconditionFailed('Cannot divide by zero');
}
return this.calculatorService.divide(<CalculatorParameters>{
intA,
intB,
});
Adding the remaining end points
Add the following end point methods inside the CalculatorController class abovethe divide end point method.
Navigation
Previous step:
Next step:Run and Test the Application