Add cors and content-type

This commit is contained in:
Michaël Lemaire 2022-09-01 23:50:17 +02:00
parent dcd0c212b9
commit 0d293c8701
2 changed files with 14 additions and 2 deletions

0
cli.ts Normal file → Executable file
View file

16
server.ts Executable file → Normal file
View file

@ -6,6 +6,13 @@ export async function processRequest(
runner = Deno.run,
fetcher = fetch,
): Promise<Response> {
if (req.method == "OPTIONS") {
return new Response(undefined, {
headers: {
"Access-Control-Allow-Origin": "*",
},
});
}
if (req.method != "GET") {
return new Response(undefined, { status: 405 });
}
@ -44,7 +51,12 @@ async function bundle(
const output = await process.output();
const status = await process.status();
if (status.success) {
return new Response(output);
return new Response(output, {
headers: {
"Content-Type": "text/javascript",
"Access-Control-Allow-Origin": "*",
},
});
} else {
return new Response(
`console.error("bundler error - Failed to bundle ${path}");`,
@ -65,7 +77,7 @@ export async function serveBundles(hostpath: string, port: number) {
const response = await processRequest(req.request, hostpath);
await req.respondWith(response);
} catch (err) {
// console.error(err);
console.error(err);
await req.respondWith(Response.error());
}
}