DNAD
Denoβ€’3y agoβ€’
3 replies
DNA

Sending proper Content-Type depending on file sent

Hello. Im new to Deno and TypeScript and i wanted to convert one of my older projects (NodeJs and JavaScript). Now, my plan is to send the requested file, which actually does work, but the response doesnt send the proper content-type. What do i have to do in order to get this working properly?

const handler = async (req: Request): Promise<Response> => {
    const url = new URL(req.url);

    const filePath: string = "./client" + (url.pathname === "/" ? "/index.html" : url.pathname);

    try {
        const file = await Deno.open(filePath, {read: true});
        const readableStream = file.readable;
        
        return new Response(readableStream, {
            status: 200,
        })
    } catch (_error) {
        return new Response(`GET ${filePath}`, {status: 404});
    }
};
Was this page helpful?