Appolo Agent
Fast and simple http server build with typescript and appolo-route
Installation:
npm install appolo-agent --save
Usage:
await ;
Agent
Options
port
- port number to agent will listen default8080
errorStack
- boolean print error stack on error defaultfalse
errorMessage
- boolean print error message defaulttrue
maxRouteCache
- max number of cached routes paths with lru cache default1000
,useRouteCache
- boolean use route path cache defaulttrue
decodeUrlParams
- boolean usedecodeURIComponent
on path params defaultfalse
qsParser
- module to use to parse querystring valuesqs
|querystring default
qs`viewEngine
- like express view wngineviewFolder
- view folder to search view paths default ``viewCache
- boolean cache viewviewExt
- views file ext defaulthtml
await port:3000 ;
get server(): http.Server | https.Server
get node http server
get router(): Router
get router instance
listen(port: number, cb?: Function): Promise<Agent>
bind the agent to port call callback if provided return the agent instance
close(): Promise<void>
close the agent connection to http server and clean everything
Routing
Static route
let app = ; ;
Parametric route
let router = ;
Wildcard route
let router = ;
Regex route
same syntax as path-to-regexp
let app = ;
get(path: string, ...handler: any[]): Agent
register new Route handler using Http.Get Method
post(path: string, ...handler: any[]): Agent
register new Route handler using Http.Post Method
put(path: string, ...handler: any[]): Agent
register new Route handler using Http.Put Method
patch(path: string, ...handler: any[]): Agent
register new Route handler using Http.Put Method
patch(path: string, ...handler: any[]): Agent
register new Route handler using Http.Patch Method
delete(path: string, ...handler: any[]): Agent
register new Route handler using Http.Delete Method
head(path: string, ...handler: any[]): Agent
register new Route handler using Http.Delete Method
add(method:Http.Methods, path: string, ...handler: any[]): Router
register new Route handler. multi methods supported
let agent = ;
Middlewares
the agent supports any express middleware or cusotom middalwares
;;; let agent = ; ;} ;
IRequest
the request object inherits from http.incomingmessage
req.query
query params object
req.body
body parser params object
req.params
route params object
req.hostname
host name of the request
req.path
path name of the request
req.secure
boolean true is the request is https
req.protocol
protocol of the request http
or https
req.app
instance of the agent
req.get(name:string)
req.header(name:string)
Returns the specified HTTP request header
req; // => "text/plain"
req.is(type:string)
Returns the matching content type if the incoming request’s “Content-Type” HTTP header field matches the MIME type specified by the type parameter. If the request has no body, returns null. Returns false otherwise.
req; // => 'html'req; // => 'text/html'
IResponse
the response object inherits from http.ServerResponse
res.status(code: number): IResponse
set response status code
resstatus200;
res.contentType(type: string): IResponse
set response content type
res.header(key: string, value: string): IResponse
res.set(key: string, value: string): IResponse
set response header
`res.cache(seconds: number): IResponse
set Cache-Control
header in seconds
res.gzip(): IResponse
compress the response with gzip and set Content-Encoding
header to gzip
res.redirect(path: string): void
redirect the request to new path
res.cookie(key: string, value: any, options?: cookie.CookieSerializeOptions): IResponse
sets cookie name to value. The value parameter may be a string or object converted to JSON.
rescookie'name' 'test' domain: '.example.com' path: '/admin' secure: true ;rescookie'someName' '{someVal:1}' expires: Date + 900000 httpOnly: true ;
res.clearCookie(key: string, options?: cookie.CookieSerializeOptions): IResponse
clears the cookie specified by name.
rescookie'name' 'tobi' path: '/admin' ;res;
json(obj: object)
sends a JSON response.
res;
jsonp(obj: object)
Sends a JSON response with JSONP support. This method is identical to res.json(), except that it opts-in to JSONP callback support
res;
`render(path: string | string[], params?: any): Promise``
render(params?: any): Promise<void>
render view html by path and params
res;res;res;
send(data?: string | Buffer| object)
res;res;res;resstatus404;resstatus500;
License
MIT