@nx.js/http
Version:
HTTP server for nx.js
42 lines (41 loc) • 1.42 kB
TypeScript
export { createStaticFileHandler, type StaticFileHandlerOptions, } from './static';
/**
* Handler function for processing an HTTP request.
*/
export type ServerHandler = (req: Request) => Response | Promise<Response>;
/**
* Options object for the {@link listen | `listen()`} function.
*/
export interface ListenOptions extends Omit<Switch.ListenOptions, 'accept'> {
fetch: ServerHandler;
}
/**
* Creates a socket handler function which accepts a socket
* event and parses the incoming data as an HTTP request.
*
* > NOTE: This is a low-level function which usually should not be used directly.
* > See {@link listen | `listen()`} instead, which is a higher-level wrapper
* > around this function.
*
* @param handler The HTTP handler function to invoke when an HTTP request is received
*/
export declare function createServerHandler(handler: ServerHandler): (event: Switch.SocketEvent) => Promise<void>;
/**
* Creates an HTTP server and listens on the `port` number specified in the options object.
* The `fetch` function will be invoked upon receiving an HTTP request.
*
* @example
*
* ```typescript
* import { listen } from '@nx.js/http';
*
* listen({
* port: 8080,
* fetch(req) {
* console.log(`Got HTTP ${req.method} request for "${req.url}"`);
* return new Response('Hello World!');
* }
* });
* ```
*/
export declare function listen(opts: ListenOptions): Switch.Server;