The server module for PXE.js.
Install @pxe/server
.
# NPM
npm i @pxe/server
# Yarn
yarn add @pxe/server
These are all @pxe/server
module methods, events and declarations.
Server
Create a server instance.
app.cb
req: http.IncomingMessage
: The requestres: http.ServerResponse
: The responseThe server callback. Can be used to create HTTP server:
const server = http.createServer(app.cb.bind(app));
// Or even shorter
const server = http.createServer(app);
app.use
...m: Middleware[]
: The middlewareAdd a middleware to the middleware list. You can add multiple middleware within a single call.
app.on
event: string
: The event namehandler: (...args: any[]) => Promise<void> | void
: The event listenerAdd an listener of an event.
app.emit
event: string
: The event name...args: any[]
: The parameter to apply to the function callPromise<void> | void | boolean
Trigger the listener of an event.
If the event listener does not exist return false
.
app.ls
port?: number
: The target porthostname?: string
: The target hostnamebacklog?: number
: The backloglisteningListener
: Runs when the server finished startinghttp.Server
Start a server listening for connections. A short way for calling:
const server = http.createServer(app);
server.listen(port, host, backlog, listeningListener);
app.icon
path: string
: The icon pathSet the icon of the app.
On each request with request URL equals /favicon.ico
the icon is returned.
app.set
T
key: string
: The keyvalue: T
: The valueSave the key value pair to the app storage.
app.get
key: string
: The keyGet the key value from the app storage.
error
err: any
: The errorctx: Context
: The context object of the current requestTrigger when an error occurred while running the middlewares.
finish
ctx: Context
: The context object of the current requestTrigger after running all the middlewares. the default event handler is the finishResponse
method in finishResponse.ts
.
RequestMethod
"GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "OPTIONS" | "PATCH" | "CONNECT" | "TRACE"
All the request method.
MIMEType
"audio/aac" | "application/x-abiword" | "application/x-freearc" | "image/avif" | "video/x-msvideo" | "application/vnd.amazon.ebook" | "application/octet-stream" | "image/bmp" | "application/x-bzip" | "application/x-bzip2" | "application/x-csh" | "application/x-cdf" | "text/css" | "text/csv" | "application/msword" | "application/vnd.ms-fontobject" | "application/gzip" | "application/vnd.openxmlformats-officedocument.wordprocessingml.document" | "application/epub+zip" | "image/gif" | "text/html" | "image/vnd.microsoft.icon" | "text/calendar" | "application/java-archive" | "image/jpeg" | "text/javascript" | "application/json" | "application/ld+json" | "audio/midi" | "audio/x-midi" | "audio/mpeg" | "video/mp4" | "application/vnd.apple.installer+xml" | "application/vnd.oasis.opendocument.presentation" | "application/vnd.oasis.opendocument.spreadsheet" | "application/vnd.oasis.opendocument.text" | "audio/ogg" | "video/ogg" | "application/ogg" | "audio/opus" | "font/otf" | "image/png" | "application/pdf" | "image/svg+xml" | "application/x-httpd-php" | "application/vnd.ms-powerpoint" | "application/vnd.rar" | "application/rtf" | "application/vnd.openxmlformats-officedocument.presentationml.presentation" | "application/x-sh" | "application/x-shockwave-flash" | "application/x-tar" | "image/tiff" | "font/ttf" | "video/mp2t" | "text/plain" | "application/vnd.visio" | "audio/wav" | "audio/webm" | "image/webp" | "font/woff" | "font/woff2" | "application/xhtml+xml" | "application/vnd.ms-excel" | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | "application/xml" | "text/xml" | "application/atom+xml" | "application/vnd.mozilla.xul+xml" | "application/zip" | "video/3gpp" | "audio/3gpp" | "video/3gpp2" | "audio/3gpp2" | "application/x-7z-compressed" | "application/x-www-form-urlencoded"
All the common MIME types.
Middleware
ctx: Context
: The current request contextnext: NextFunction
: The function that calls the next middleware...args: any[]
: The arguments passed by the previous middlewareThe middleware type.
NextFunction
..args: any[]
: The arguments to pass to the next middlewareThe next function type.
Context
All the properties of a ctx
object.
All the properties of ctx.request
.
request.raw
http.IncomingMessage
The raw HTTP request object. This property is read-only.
request.method
Server.RequestMethod
The request method. This property is read-only.
request.url
string
This property is read-only. The request URL.
request.headers
http.IncomingHttpHeaders
This property is read-only. Parsed request headers.
request.body
Promise<any>
Get the request body.
request.query
Record<string, string>
This property is read-only. The parsed request query.
All the properties and methods of ctx.response
.
response.raw
http.ServerResponse
The raw HTTP response object. This property is read-only.
response.body
any
The response body.
Modify this to modify the response.
If response.body
, response.status.code
and response.status.message
are not set a 404 error is returned to the client.
response.type
Server.MIMEType
The content type of the response. If this property is not set the content type is chosen base on the response.
response.status
code?: number
: The status code.message?: string
: The status message.Set the status code and status message. This property is read-only.
response.headers
Record<string, string | readonly string[] | number>
Set the response headers. This property is read-only.
You cannot directly set the headers using response.headers = ...
.
Instead use Object.assign(response.headers, ...)
.
response.redirect
url: string
: The target URLpermanent?: boolean
: Whether this redirect is a permanent redirect or notRedirect to different URL.
All the properties and methods of ctx.cookie
.
cookie.options
Server.CookieOptions
The cookie options.
cookie.value
string
The cookie value in string
. Modify this property to modify the cookie value.
cookie.remove
Remove the cookie. After this call cookie.value
cannot be used to set cookie value and cookie.removed
is set to true
.
cookie.removed
boolean
Check whether the request cookie is removed.
cookie.iv
Buffer
The cookie initialization vector.
cookie.key
string
The cookie generated secret key.