D
Deno

help

Serving static website

NRNiranjan Ramesh12/8/2023
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?
NNeTT12/8/2023
does your server also serve the main.js file and css file using Deno.readFile()?
Llcasdev12/8/2023
@LuckyCoder you can use https://deno.land/std@0.208.0/http/file_server.ts?s=serveDir to serve an entire directory of files
NRNiranjan Ramesh12/8/2023
Thanks for this 🙂 I will look into this. Certainly saves lines of code.
ABAltair 680b12/8/2023
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
})
NRNiranjan Ramesh12/9/2023
Thanks 🙂

Looking for more? Join the community!

Recommended Posts
positional arguments deno taskCan I use positional arguments in deno task? I have a number of scripts that I frequently want to rLinter does not warn about lack of constructor arguments.The vscode shows that there are no arguments, but linter does not show it as an error: ``` root@instCan you make a fresh route detect, if it is being requested by `deno run`Can you make a fresh route detect, if it is being requested by `deno run` like `deno run https://locDeno dependency top level await work aroundI have the following error while building with dnt ``` [dnt] Top level await cannot be used when disBundling a CLI written in JS with DenoHi team, Thanks for writing this [excellent blog-post (and video)](https://deno.com/blog/roll-your-Leaking operation op_http_close in testsI tried to update my repositories from Deno 1.38.1 to 1.38.4. After the update my test keeps failingDoes BroadcastChannel preserve the message order if the receiver is in a different geo-location?Does BroadcastChannel preserve the message order if the receiver is in a different geo-location?NPM CCXT Not WorkingI'm trying to run the NPM Package CCXT and I'm getting an `"ERR_MODULE_NOT_FOUND"` error any help How do you use a private docker hub image on AWS Lightsail?I'm following the guide here https://docs.deno.com/runtime/manual/advanced/deploying_deno/aws_lightsIs there a way to reset/flush my deno vk queues on deploy?Nothing seems to be queuing. I would like to reset it if possible?Return lazy struct fields from OpMy goal is to build a plugin system which allows invoking user defined code. One of the features I aRelative path in WebSocketWhy does relative path in `WebSocket` not work?