gpt4 book ai didi

express - 类型错误 : Failed to execute 'compile' on 'WebAssembly' : Incorrect response MIME type. 预期为 'application/wasm'

转载 作者:行者123 更新时间:2023-12-03 20:53:54 35 4
gpt4 key购买 nike

我正在尝试使用 Chrome 上的 fetch api 加载 .wasm 文件,并使用 express 提供 html 文件。但是 chrome 不允许我加载文件:

'Uncaught (in promise) TypeError: Failed to execute 'compile' on 'WebAssembly': 不正确的响应 MIME 类型。预期的“应用程序/wasm”。

这是我的文件:

/public/index.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
WebAssembly.instantiateStreaming(fetch('http://localhost:3000/simple.wasm'))
.then(obj => {
console.log(obj.instance.exports.add(1, 2)); // "3"
});
</script>
</body>
</html>

express 服务:

/index.js
const express = require('express')
express.static.mime.define({'application/wasm': ['wasm']})
var app = express();

app.use('/', express.static('public'));

app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})

我可以在提供 .wasm 文件时添加新的 mime 类型来表达吗?
还是让chrome接受?
我不知道如何解决它^^

见: http://kripken.github.io/emscripten-site/docs/compiling/WebAssembly.html
Web server setup
To serve wasm in the most efficient way over the network, make sure your web server has the proper MIME time for .wasm files, which is application/wasm. That will allow streaming compilation, where the browser can start to compile code as it downloads.

In Apache, you can do this with

AddType application/wasm .wasm
Also make sure that gzip is enabled:

AddOutputFilterByType DEFLATE application/wasm

谢谢大家!

最佳答案

一种可能的解决方法是使用 instantiate()而不是 instantiateStreaming() ,因为前者不关心 MIME 类型(而后者 does)。使用 instantiate() :

async function fetchAndInstantiate() {
const response = await fetch("http://localhost:3000/simple.wasm");
const buffer = await response.arrayBuffer();
const obj = await WebAssembly.instantiate(buffer);
console.log(obj.instance.exports.add(1, 2)); // "3"
}

关于express - 类型错误 : Failed to execute 'compile' on 'WebAssembly' : Incorrect response MIME type. 预期为 'application/wasm',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50589083/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com