api proxy & file listing
This commit is contained in:
parent
5cc652b0af
commit
db68058d5b
5 changed files with 96 additions and 35 deletions
|
@ -1,12 +1,26 @@
|
|||
import type { RequestHandler } from '@builder.io/qwik-city';
|
||||
import ky from 'ky';
|
||||
import type { RequestEvent, RequestHandler } from '@builder.io/qwik-city';
|
||||
|
||||
export const onGet: RequestHandler = async ({ send, pathname, request }) => {
|
||||
const res = await ky.get(`http://localhost:8081${pathname}`, {
|
||||
const proxy = async ({ send, url, pathname, request }: RequestEvent) => {
|
||||
const targetUrl = new URL(`http://localhost:8081${pathname}`, url);
|
||||
const headers = new Headers(request.headers);
|
||||
|
||||
const backendResponse = await fetch(targetUrl, {
|
||||
method: request.method,
|
||||
headers: request.headers,
|
||||
body: request.method === 'GET' ? null : request.body,
|
||||
headers,
|
||||
body: request.method !== 'GET' && request.method !== 'HEAD' ? request.body : undefined,
|
||||
redirect: 'manual',
|
||||
});
|
||||
|
||||
send(res);
|
||||
};
|
||||
console.log(`Proxying ${request.method} request to: ${targetUrl.href}`);
|
||||
|
||||
send(
|
||||
new Response(backendResponse.body, {
|
||||
status: backendResponse.status,
|
||||
statusText: backendResponse.statusText,
|
||||
headers: backendResponse.headers,
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
export const onGet: RequestHandler = proxy;
|
||||
export const onPost: RequestHandler = proxy;
|
Loading…
Add table
Add a link
Reference in a new issue