Skip to content

Instantly share code, notes, and snippets.

@GerritRiesch94
Last active March 27, 2023 13:23
Show Gist options
  • Save GerritRiesch94/044bb0ec75ccfbde3e5c57d021ec6e8a to your computer and use it in GitHub Desktop.
Save GerritRiesch94/044bb0ec75ccfbde3e5c57d021ec6e8a to your computer and use it in GitHub Desktop.
Nest.js Controller Example
import { Controller, Get, Param, Query, Res } from '@nestjs/common';
import { AppService } from './app.service';
import { Response } from 'express';
@Controller('api/data')
export class AppController {
constructor(private readonly appService: AppService) {}
@Get(':id')
async getData(
@Param('id') id: string,
@Query('skip') skip: number,
@Query('top') top: number,
@Res() response: Response,
): Promise<void> {
// should represent a database service, api call, etc.
const data = this.appService.searchForIdAndPaginate(id, skip, top);
if (data.length > 0) {
response.status(200).send(data);
} else {
response.status(204).send();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment