Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satishbabariya/adebf89e872eb673e3056b9df278e0ad to your computer and use it in GitHub Desktop.
Save satishbabariya/adebf89e872eb673e3056b9df278e0ad to your computer and use it in GitHub Desktop.
import "reflect-metadata";
import resty, {
Controller,
Get,
Request,
Response,
NextFunction,
Post,
Body,
Put,
Context,
Param,
Query,
ValidationError,
HTTPError,
} from "@restyjs/core";
import { IsString } from "class-validator";
class PostDTO {
@IsString()
title!: string;
@IsString()
content!: string;
}
@Controller("/")
class HelloController {
@Get("/")
index() {
return "Hello World";
}
@Get("/search")
search(@Query("name") name: string, @Query("email") email: string) {
return {
name,
email,
};
}
@Post("/")
async create(ctx: Context, @Body() body: PostDTO) {
return { body: body };
}
@Put("/")
async update(@Body() body: PostDTO, ctx: Context) {
return ctx.res.json({ body: body }).status(200);
}
@Get("/rest")
async rest() {
return "Hello rest";
}
@Get("/error")
error() {
throw new Error("sample error");
}
@Get("/health")
health(ctx: Context) {
return ctx.res.json({ status: "ok" }).status(200);
}
@Post("/health")
postHealth(ctx: Context) {
return ctx.res.json({ status: "ok" }).status(200);
}
@Get("/:id")
getByID(@Param("id") id: string) {
return {
id: id,
};
}
}
const app = resty({
controllers: [HelloController],
});
app.listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment