servax
v1.0.6
Published
Typescript HTTP Framework
Downloads
10
Maintainers
Readme
Servax
Servax is a framework for building server side web applications in TypeScript and makes use of Experimental Decorators.
Servax focuses on the ability to structure and implement application code and leaves the underling both HTTP communication and view templating to other more specialist libraries, e.g. ExpressJS and Nunjucks.
Installation
Install using NPM
npm i servax
Install a HTTP Host e.g.
npm i servax.express
Install a View Renderer e.g.
npm i servax.nunjucks
Usage
@Dependency("SILLY_SERVICE", DependencyLifespan.PerRequest)
class SillyService {
public helloWorld(): string {
return "Hello, World!";
}
}
@Controller()
class BasicController {
constructor(
@Depends("SILLY_SERVICE") private readonly sillyService: SillyService,
) {}
@Get()
public basicMethod(): TextResponse {
return new TextResponse(
this.sillyService.helloWorld(),
);
}
}
const httpServer = new HttpServer();
httpServer
.setHttpHost(new ExpressHttpHost())
.setViewRenderer(new NunjucksViewRenderer(), {
root: "./views",
} as IViewRendererOptions)
.registerServices([ SillyService ])
.registerControllers([ BasicController ])
.start({
port: 80,
} as IHttpHostOptions);
Routing
Routing can be applied at Controller Level or Verb Level. Controller and Verb routes are combined, e.g.
@Controller('foo')
class Controller {
// This method will be available via:
// GET <server>:<port>/foo/bar
@Get('bar')
public Bar(): TextResponse {
// ...
}
}
Supported Verbs:
GET
POST
PUT
DELETE
PATCH
OPTIONS
ALL
Built-in Response Types
TextResponse
HtmlResponse
ViewResponse < Requires Registering a View Resolver such as servax.nunjucks
Dependency Lifetimes
DependencyLifespan.PerRequest; // Recreated for every server request that needs it
DependencyLifespan.ApplicationLifetime; // Created once and reused