我正在尝试使用 Chrome 上的 fetch api 加载 .wasm 文件,并使用 express 提供 html 文件。但 chrome 不允许我加载该文件:
'未捕获(承诺中)类型错误:无法在'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>
快递服务:
/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接受它?
我不知道如何解决它^^
See: http://kripken.github.io/emscripten-site/docs/compiling/WebAssembly.html 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
谢谢大家!