templates - 资源解释为样式表,但使用 MIME 类型 text/html 传输:“http://localhost:4000/style.css”
问题描述
我正在使用 deno、橡木和denjucks。文件结构如下 -
index.html
index.ts
style.css
我使用的代码如下 - index.ts 是 -
import {Application, send} from "https://deno.land/x/oak/mod.ts";
import denjucks from "https://deno.land/x/denjucks/mod.js";
const app = new Application();
const HOST = "http://localhost";
denjucks.configure('', { autoescape: true });
const PORT = 4000;
let res = denjucks.render("index.html", {txt: "World"});
app.use(async (context) => {
context.response.body = res;
});
console.log(`Server started at: ${HOST}:${PORT}`);
await app.listen({ port: PORT });
index.html -
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./style.css" type="text/css">
<title>Document</title>
</head>
<body>
<h1>{{txt}}</h1>
<h1>Hello World</h1>
</body>
</html>
样式.css -
body{
background: tomato;
}
当我执行代码 deno run --allow-all index.ts 时,在 chrome 开发人员工具控制台中出现警告消息 -资源解释为样式表,但使用 MIME 类型 text/html 传输:“ http://localhost:4000/style. .css ”。并且css代码不适用。此故障背后的原因尚不清楚。
解决方案
你所有的路线都在服务text/html
,因为你总是回应denjucks.render("index.html", {txt: "World"});
,所以当你请求时,style.css
你会回应index.html
您需要设置一个静态文件中间件,以便./style.css
正确提供服务。
app.use(async (context) => {
await send(context, context.request.url.pathname, {
root: `${Deno.cwd()}`
});
});
app.use(async (context) => {
context.response.body = res;
});
推荐阅读
- amazon-web-services - 如何在 cloudformation 模板的输出部分返回 emr 集群 ID
- c# - 使用枚举字节而不是 int 有什么意义?
- ios - 如何在 WatchOS 的基于页面的界面中显示警报?
- c++ - 如何将“使用 NodeMCU 的 Blynk 代码”转换为“使用 ArduinoUno 的 Blynk 代码”
- android - 如何在 Android 上使用谷歌地图 SDK 显示自行车道?
- php - 不能多次引用变量名“id”
- mysql - 如何在多个记录和多个字段上连接表
- javascript - Instagram 登录字段未在桌面版 Internet Explorer 11 中激活
- c - 在 C 中使用 lseek 系统调用来读取写入文件中的结构
- java - Android studio Java - 地图的底页视图