Reputation: 2760
My script as follows, which compiles without errors, is suppose to serve index.html, however nothing is ever sent to the browser while the page is showing it's loading.
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { serveFile } from 'https://deno.land/[email protected]/http/file_server.ts';
const server = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of server) {
console.log(req.url);
if(req.url === '/')
await serveFile(req, 'index.html');
}
So why is serveFile not working in this instance?
Upvotes: 2
Views: 669
Reputation: 22535
The call to serveFile
only creates a Response
(status, headers, body) but doesn't send it.
You have to send it with a separate call to req.respond()
:
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { serveFile } from 'https://deno.land/[email protected]/http/file_server.ts';
const server = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of server) {
console.log(req.url);
if(req.url === '/') {
const response = await serveFile(req, 'index.html');
req.respond(response)
}
}
Upvotes: 1