Routing Controller
Make Controller as Router with simple Decorators.
Config
{
"compilerOptions":{
"experimentalDecorators": true,
}
}
tsconfig.json
{
"compilerOptions": {
"moduleResolution": "nodenext",
"experimentalDecorators": true,
"target": "ES5",
"lib": [
"DOM",
"DOM.Iterable",
"ESNext"
]
},
}
Usage
import { nhttp, RequestEvent } from "@nhttp/nhttp";
import { Get, Controller } from "@nhttp/nhttp/controller";
@Controller("/hello")
class HelloController {
@Get("/")
hello(rev: RequestEvent) {
console.log(rev.url);
return "Hello, World";
}
}
const app = nhttp();
app.use("/api/v1", new HelloController());
// or multi controllers
// app.use("/api/v1", [new HelloController(), new HomeController()]);
app.listen(8000);
@Controller
Controller decorators.
@Controller()
// or
@Controller("/hello")
// or
@Controller("/hello", ...middlewares);
@Controller("/hello");
class HelloController {...}
@[Method]
Method Decorators. Get | Post | Delete
and more methods.
@[Method](path_string);
@Controller("/hello");
class HelloController {
// GET/hello/123
@Get("/:id")
findById(rev: RequestEvent) {...}
// POST/hello
@Post("/")
create(rev: RequestEvent) {...}
}
@Wares
Middleware decorators.
@Wares(...middlewares);
class UserMiddleware {
use(rev: RequestEvent, next: NextFunction) {
rev.user = "john";
return next();
}
}
@Controller("/hello");
class HelloController {
@Wares(UserMiddleware)
@Get("/")
getUser(rev: RequestEvent) {
return rev.user;
// => john
}
}
@Upload
Upload decorators. related to Upload Middleware
@Upload(config);
@Controller("/hello");
class HelloController {
@Upload({ name: "image" })
@Post("/")
hello(rev: RequestEvent) {
console.log(rev.file);
return "success upload";
}
}
@Status
Status decorators. set statusCode in decorators.
@Status(code);
@Controller("/hello");
class HelloController {
@Status(201)
@Post("/")
hello(rev: RequestEvent) {
return "success save with status " + rev.response.statusCode;
}
}
@Header
Header decorators. set Header in decorators.
@Header(key, val);
@Header(obj);
@Controller("/hello");
class HelloController {
@Header("name", "john")
@Get("/")
hello(rev: RequestEvent) {
return "hello";
}
}
@Type
Type decorators. set Content-Type in decorators.
@Type(contentType);
@Controller("/hello");
class HelloController {
@Type("html")
@Get("/")
hello(rev: RequestEvent) {
return "<h1>Hello, World</h1>";
}
}
@View
View decorators. set View in decorators. requires app.engine
configs.
@View(name);
@Controller("/hello");
class HelloController {
@View("index")
@Get("/")
hello(rev: RequestEvent) {
// set params
return { title: "Hello World" };
}
}
app.engine
import ejs from "npm:ejs";
app.engine(ejs.renderFile, { base: "views", ext: "ejs" });
@Jsx
Jsx decorators. requires app.engine
to React / Preact.
@Jsx();
@Controller("/hello");
class HelloController {
@Jsx()
@Get("/")
hello(rev: RequestEvent) {
// set params
// rev.response.params = { title: "Hello Jsx" };
// return as jsx
return <h1>Hello, Jsx</h1>;
}
}
app.engine
import React from "npm:react";
import { renderToString } from "npm:react-dom/server";
app.engine((elem, params) => {
return `
<!DOCTYPE html>
<html>
<head>
<title>${params.title}</title>
</head>
<body>
${renderToString(elem)}
</body>
</html>
`;
});