Created
June 21, 2020 19:44
-
-
Save satishbabariya/adebf89e872eb673e3056b9df278e0ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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