Niranjan Ramesh
Niranjan Ramesh•13mo ago

Serving static website

I have a static website with an index.html that imports a main.js and css file. When serving index.html with Deno using Deno.readFile("./dist/index.html"), I get a blank page with errors in the console for the main.js file that it references. Do I have to define all paths and return the corresponsing paths? Is there no straightforward way to do this by just passing the directory?
5 Replies
NeTT
NeTT•13mo ago
does your server also serve the main.js file and css file using Deno.readFile()?
lcasdev
lcasdev•13mo ago
@LuckyCoder you can use https://deno.land/std@0.208.0/http/file_server.ts?s=serveDir to serve an entire directory of files
Niranjan Ramesh
Niranjan RameshOP•13mo ago
Thanks for this 🙂 I will look into this. Certainly saves lines of code.
Fifth-Normal-Form
Fifth-Normal-Form•13mo ago
Or this for a little more control.
import { serveFile } from "https://deno.land/std@0.203.0/http/file_server.ts"

const port = 8080

Deno.serve({port:port}, async (request: Request): Promise<Response> => {
let { pathname } = new URL(request.url);
if (pathname === '/') pathname = '/index.html';
console.log('Serving: ', pathname)
const resp = await serveFile(request, '.' + pathname )
return resp
})
import { serveFile } from "https://deno.land/std@0.203.0/http/file_server.ts"

const port = 8080

Deno.serve({port:port}, async (request: Request): Promise<Response> => {
let { pathname } = new URL(request.url);
if (pathname === '/') pathname = '/index.html';
console.log('Serving: ', pathname)
const resp = await serveFile(request, '.' + pathname )
return resp
})
Niranjan Ramesh
Niranjan RameshOP•13mo ago
Thanks 🙂

Did you find this page helpful?