11import express , { Request , Response } from 'express' ;
2- import { request as httpsRequest } from 'https' ;
3- import { request as httpRequest } from 'http' ;
2+ import {
3+ request as httpsRequest ,
4+ RequestOptions as HttpsRequestOptions ,
5+ } from 'https' ;
6+ import {
7+ request as httpRequest ,
8+ RequestOptions as HttpRequestOptions ,
9+ } from 'http' ;
410import { PassThrough } from 'stream' ;
511import {
612 formatRequestBody ,
@@ -22,8 +28,9 @@ export function startServer({ cliOptions }: { cliOptions: CliOptions }) {
2228 }
2329
2430 // Setup options for the outbound request
25- const options = {
31+ const options : HttpRequestOptions | HttpsRequestOptions = {
2632 method : req . method ,
33+ // Headers: override the host header for the target.
2734 headers : { ...req . headers , host : targetUrl . host } ,
2835 } ;
2936
@@ -87,15 +94,28 @@ export function startServer({ cliOptions }: { cliOptions: CliOptions }) {
8794
8895 app . all ( '*' , ( req : Request , res : Response ) => {
8996 console . log ( `Hitting catch all route: ${ req . method } ${ req . path } ` ) ;
97+
98+ const targetUrlParam = req . query . target_url as string ;
99+ const targetUrl = parseTargetUrl ( targetUrlParam ) ;
100+ if ( targetUrl ) {
101+ const options : HttpRequestOptions | HttpsRequestOptions = {
102+ method : req . method ,
103+ // Headers: override the host header for the target.
104+ headers : { ...req . headers , host : targetUrl . host } ,
105+ } ;
106+ console . log ( 'options' , options ) ;
107+ }
108+
109+ // Setup options for the outbound request
110+ const fullUrl = `${ req . protocol } ://${ req . get ( 'host' ) } ${ req . originalUrl } ` ;
111+ console . log ( 'Full URL:' , fullUrl ) ;
112+ console . log ( 'Full hostname:' , req . hostname ) ;
113+ console . log ( 'Subdomains:' , req . subdomains ) ;
114+
90115 res . send ( 'Noting to see here. Try POST /chat/completions' ) ;
91116 } ) ;
92117
93118 app . listen ( PORT , ( ) => {
94119 console . log ( `Server running on port ${ PORT } ` ) ;
95- console . log ( `Try sending a cURL request:\n` ) ;
96- console . log ( `
97- curl -X POST \\
98- http://localhost:${ PORT } /?target_url=https://jsonplaceholder.typicode.com/posts \\
99- -d '{"title": "foo", "body": "bar", "userId": 1}'` ) ;
100120 } ) ;
101121}
0 commit comments