Typescript 如何使用serveFile为Deno中的文件提供服务?

Typescript 如何使用serveFile为Deno中的文件提供服务?,typescript,localhost,deno,Typescript,Localhost,Deno,我的脚本如下所示,编译时没有错误,假设它是为index.html服务的,但是当页面显示加载时,不会向浏览器发送任何内容 import { serve } from "https://deno.land/std@0.91.0/http/server.ts"; import { serveFile } from 'https://deno.land/std@0.91.0/http/file_server.ts'; const server = serve({ port: 800

我的脚本如下所示,编译时没有错误,假设它是为index.html服务的,但是当页面显示加载时,不会向浏览器发送任何内容

import { serve } from "https://deno.land/std@0.91.0/http/server.ts";
import { serveFile } from 'https://deno.land/std@0.91.0/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');
}

那么为什么serveFile在这个实例中不起作用呢?

serveFile
的调用只创建一个(状态、标题、正文),而不发送它

您必须单独调用
req.respond()

import { serve } from "https://deno.land/std@0.91.0/http/server.ts";
import { serveFile } from 'https://deno.land/std@0.91.0/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)
  }
}