[英]Set MIME type for fetched file
我有一个静态文件服务器(由vibe.d制造),为使用ES6模块但扩展名为.mjs的网站提供服务。
我的浏览器(Arch Linux上的Chromium)在获取server responded with a non-JavaScript MIME type of "application/octet-stream"
的模块文件server responded with a non-JavaScript MIME type of "application/octet-stream"
时,抛出了错误。
看来我需要使用.mjs从“ application / octet-stream”到“ application / javascript”设置MIME类型文件。 我该怎么做呢? 我可以将所有脚本更改为.js
但是那是我想弄清楚如何正确解决。
如何更改要提取的文件的MIME类型? 也许更好,我可以更改所有.mjs文件的默认MIME类型吗?
这是我与vibe.d的d代码:
auto router = new URLRouter;
auto fileServerSettings = new HTTPFileServerSettings;
fileServerSettings.encodingFileExtension = ["gzip" : ".gz"];
router.get("/gzip/*", serveStaticFiles("./public/", fileServerSettings));
router.get("/ws", handleWebSockets(&handleWebSocketConnection));
router.get("*", serveStaticFiles("./public/",));
listenHTTP(settings, router);
响应中的内容类型标头需要更改。
Vibe.d可能有一种配置默认值的方法,但是您始终可以在发送响应以编辑以.mjs
结尾的文件头之前捕获它。
您可以在vibe.d中执行以下操作:
auto router = new URLRouter;
auto fileServerSettings = new HTTPFileServerSettings;
fileServerSettings.encodingFileExtension = ["gzip" : ".gz"];
fileServerSettings.preWriteCallback = &handleMIME; // Add preWriteCallback to fileServerSettings
router.get("/gzip/*", serveStaticFiles("./public/", fileServerSettings));
router.get("/ws", handleWebSockets(&handleWebSocketConnection));
router.get("*", serveStaticFiles("./public/", fileServerSettings)); // Use fileServerSettings in this get too.
// preWriteCallback, will edit the header before vibe.d sends it.
void handleMIME(scope HTTPServerRequest req, scope HTTPServerResponse res, ref string physicalPath) {
if (physicalPath.endsWith(".mjs")) {
res.contentType = "application/javascript"; // vibe.d has an easy `.contentType` attribute so you do not have to deal with the header itself.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.