如何解决如何使用 serveFile 在 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: 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
的调用仅创建一个 Response
(状态、标题、正文)但不会发送它。
您必须通过单独调用 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)
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。