从NestJs后端加载WebAssembly文件时出错
我正在尝试将WebAssembly文件加载到浏览器中。我正在尝试加载库,当我尝试执行所描述的操作时,我得到一个错误从NestJs后端加载WebAssembly文件时出错,nestjs,webassembly,Nestjs,Webassembly,我正在尝试将WebAssembly文件加载到浏览器中。我正在尝试加载库,当我尝试执行所描述的操作时,我得到一个错误 const worker = new Worker('http://localhost:3000/webworker-wasm') VM114:1 Refused to create a worker from 'http://localhost:3000/webworker-wasm' because it violates the following Content Secur
const worker = new Worker('http://localhost:3000/webworker-wasm')
VM114:1 Refused to create a worker from 'http://localhost:3000/webworker-wasm' because it violates the following Content Security Policy directive: "default-src 'none'". Note that 'worker-src' was not explicitly set, so 'default-src' is used as a fallback.
所以我想这与我如何提供文件有关。我有一个后端,为该文件提供服务的代码如下
@Get('webworker-wasm')
private getWebworkerWasm(req: Request, res: Response) {
fs.readFile('./node_modules/stockfish.js/stockfish.wasm.js', (err: any, data: Buffer) => {
res.writeHead(200, {'Content-Type': 'application/wasm'});
res.status(200).end(data);
});
}
此设置中是否有不正确的内容或我是否忘记了什么
更新:我对代码做了一些修改,现在错误有点不同了
fs.readFile('./node_modules/stockfish.js/stockfish.wasm.js', 'binary', (err: any, data: Buffer) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Request-Method', '*');
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
res.setHeader('Access-Control-Allow-Headers', '*');
res.setHeader('Content-Type', 'application/wasm');
// res.writeHead(200, {'Content-Type': 'application/wasm'});
res.status(200).end(data);
});
给出:
x = new Worker('http://localhost:3000/files/webworker-wasm');
VM48:1 Uncaught DOMException: Failed to construct 'Worker': Script at 'http://localhost:3000/files/webworker-wasm' cannot be accessed from origin 'chrome-search://local-ntp'.
at <anonymous>:1:5
我的情况很好,如果我可以解决这个与铬设置 我无法添加评论,因此我将此作为答案发布 请发布浏览器中显示的所有响应标题
另外,您将JavaScript文件作为WebAssembly提供,application/wasm应该是application/JavaScript。您是对的。它是javascript。结果是javascript加载了wasm文件