UNPKG

@teclone/r-server

Version:

A lightweight, extensible web-server with inbuilt routing-engine, static file server, file upload handler, request body parser, middleware support and lots more

208 lines (207 loc) 6.76 kB
import { Router } from './Router'; import { RServerConfig, Method, Callback, Middleware, RouteId, MiddlewareId, ErrorCallback, Env } from '../@types'; import { AddressInfo } from 'net'; import { Wrapper } from './Wrapper'; import { Http1Response, Http2Response } from './Response'; import { Http1Request, Http2Request } from './Request'; export interface ServerConstructorOptions<Http1Rq extends typeof Http1Request, Http2Rq extends typeof Http2Request, Http1Rs extends typeof Http1Response, Http2Rs extends typeof Http2Response> { configFile?: string; config?: RServerConfig; /** * routing base path */ basePath?: string; Http1ServerRequest?: Http1Rq; Http2ServerRequest?: Http2Rq; Http1ServerResponse?: Http1Rs; Http2ServerResponse?: Http2Rs; } export declare class Server<Http1Rq extends typeof Http1Request = typeof Http1Request, Http2Rq extends typeof Http2Request = typeof Http2Request, Http1Rs extends typeof Http1Response = typeof Http1Response, Http2Rs extends typeof Http2Response = typeof Http2Response> { private server; private secureServer; private entryPath; private config; private router; private mountedRouters; private logger; private bodyParser; private fileServer; private errorCallback; readonly env: Env; constructor(options?: ServerConstructorOptions<Http1Rq, Http2Rq, Http1Rs, Http2Rs>); /** * resolves and merges the configuration objects */ private resolveConfig; private createServers; /** * server root directory */ get rootDir(): string; /** * load env settings */ private loadEnv; /** * returns server intro */ private getServerIntro; /** * cordinates how routes are executed, including mounted routes */ private cordinateRoutes; /** * perform house keeping */ private onResponseFinish; /** * parse all request data */ private parseRequestData; /** * handle onrequest end event */ private onRequestEnd; /** * handle request data event */ private onRequestData; /** * handles onrequest events */ private onRequest; /** * binds all event handlers on the server */ private closeServer; /** * binds all event handlers on the server */ private initServer; /** * returns boolean indicating if the server is listening */ get listening(): boolean; /** * returns the server instance router */ getRouter(): Router; /** * returns the server instance mounted routers */ getMountedRouters(): Router[]; /** * returns the resolved server config object */ getConfig(): Required<RServerConfig>; /** * sets routing base path that gets prepended to all route and middleware urls */ setBasePath(basePath: string): void; /** * sets the app intance callback error handler * @param errorCallback app instance error callback */ setErrorCallback(errorCallback: ErrorCallback): void; /** * stores route rules for http OPTIONS method * * @param url - route url * @param callback - route callback handler * @param options - route configuration object or middleware or array of middlewares */ options(path: string, callback: Callback, use?: Middleware | Middleware[]): number; /** * stores route rules for http HEAD method * * @param url - route url * @param callback - route callback handler * @param options - route configuration object or middleware or array of middlewares */ head(path: string, callback: Callback, use?: Middleware | Middleware[]): number; /** * stores route rules for http GET method * * @param url - route url * @param callback - route callback handler * @param options - route configuration object or middleware or array of middlewares */ get(path: string, callback: Callback, use?: Middleware | Middleware[]): number; /** * stores route rules for http POST method * * @param url - route url * @param callback - route callback handler * @param use - route configuration object or middleware or array of middlewares */ post(path: string, callback: Callback, use?: Middleware | Middleware[]): number; /** * stores route rules for http PUT method * @param url - route url * @param callback - route callback handler * @param options - route configuration object or middleware or array of middlewares */ put(path: string, callback: Callback, use?: Middleware | Middleware[]): number; /** * stores route rules for http DELETE method * * @param url - route url * @param callback - route callback handler * @param options - route configuration object or middleware or array of middlewares */ delete(path: string, callback: Callback, use?: Middleware | Middleware[]): number; /** * stores route rules for all http methods * * @param url - route url * @param callback - route callback handler * @param options - route configuration object or middleware or array of middlewares */ any(path: string, callback: Callback, use?: Middleware | Middleware[]): void; /** * returns a route wrapper for the given url */ route(path: string): Wrapper; /** * removes a given route * @param id route id */ removeRoute(id: RouteId): boolean; /** * removes a given middleware * @param id middleware id */ removeMiddleware(id: MiddlewareId): boolean; /** * registers a middleware to be called whenever the given url is visited * * @param url - url to apply middleware to. use * to appy to all urls * @param middleware - the middleware or array of middlewares * @param options - middleware configuration option. here, you can specify the http method * that the middleware will run against */ use(path: string, middleware: Middleware | Middleware[], operation?: Method | Method[]): number; /** * mounts a router to the server instance */ mount(basePath: string, router: Router): void; /** * starts the server at a given port */ listen(port?: number | { httpPort?: number; httpsPort?: number; }): Promise<boolean>; /** * closes server * @param callback callback function to execute when connection closes */ close(): Promise<boolean>; /** * returns http and https server address */ address(): { http: AddressInfo | null; https: AddressInfo | null; }; }