- Notifications
You must be signed in to change notification settings - Fork 10.6k
/
Copy pathserver.ts
41 lines (36 loc) · 1.31 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
importtype{RequestListener}from"node:http";
importtype{
AppLoadContext,
ServerBuild,
UNSAFE_MiddlewareEnabled,
unstable_InitialContext,
}from"react-router";
import{createRequestHandler}from"react-router";
importtype{ClientAddress}from"@mjackson/node-fetch-server";
import{createRequestListenerascreateRequestListener_}from"@mjackson/node-fetch-server";
typeMaybePromise<T>=T|Promise<T>;
exportinterfaceRequestListenerOptions{
build: ServerBuild|(()=>ServerBuild|Promise<ServerBuild>);
getLoadContext?: (
request: Request,
client: ClientAddress
)=>UNSAFE_MiddlewareEnabledextendstrue
? MaybePromise<unstable_InitialContext>
: MaybePromise<AppLoadContext>;
mode?: string;
}
/**
* Creates a request listener that handles requests using Node's built-in HTTP server.
*
* @param options Options for creating a request listener.
* @returns A request listener that can be used with `http.createServer`.
*/
exportfunctioncreateRequestListener(
options: RequestListenerOptions
): RequestListener{
lethandleRequest=createRequestHandler(options.build,options.mode);
returncreateRequestListener_(async(request,client)=>{
letloadContext=awaitoptions.getLoadContext?.(request,client);
returnhandleRequest(request,loadContext);
});
}