uWebSockets.js adapter for Hattip
npm install @hattip/adapter-uwebsockets@hattip/adapter-uwebsocketsHattip adapter for uWebSockets.js on Node.js.
``js
import { createServer } from "@hattip/adapter-uwebsockets";
import handler from "./handler.js";
createServer(handler).listen(3000, "localhost", () => {
console.log("Server listening on http://localhost:3000");
});
`
`ts
/**
* Create an HTTP server
*/
function createServer(
handler: Handler,
adapterOptions?: UWebSocketsAdapterOptions,
appOptions?: appOptions,
): TemlateApp;
/* Adapter options /
export interface UWebSocketAdapterOptions {
/**
* Set the origin part of the URL to a constant value.
* It defaults to process.env.ORIGIN. If neither is set,req.protocol
* the origin is computed from the protocol and hostname.
* To determine the protocol, is tried first.trustProxy
* If is set, X-Forwarded-Proto header is used.req.socket.encrypted
* Otherwise, is used.X-Forwarded-Host
* To determine the hostname, trustProxy
* (if is set) or Host header is used.X-Forwarded-
*/
origin?: string;
/**
Whether to trust headers. X-Forwarded-ProtoX-Forwarded-Host
* and are used to determine the origin whenorigin
* and process.env.ORIGIN are not set. X-Forwarded-Forprocess.env.TRUST_PROXY
* is used to determine the IP address. The leftmost values are used
* if multiple values are set. Defaults to true if 1
* is set to , otherwise false.`
*/
trustProxy?: boolean;
/* Use SSL (https) /
ssl?: boolean;
/* Static file directory /
staticDir?: string;
/**
* Callback to configure the uWebSockets.js app.
* Useful for adding WebSocket or HTTP routes before the Hattip handler
* is added.
*/
configureServer?: (app: TemplatedApp) => void;
}
This adapter is experimental. In particular, the static file server is very limited at the moment.
This adapter uses node-fetch if the fetch global is not available. You can disable this behavior by importing your adapter from @hattip/adapter-uwebsockets/native-fetch, which will throw an error if fetch is not available.
This adapter makes uWebSockets.js request (HTTPRequest) and response (HTTPResponse) objects available in context.platform`.