我在尝试Deno 的渐进式 HTTP 服务器 Servest ,在运行示例时遇到错误:
$ deno run Servest.ts
error: Uncaught PermissionDenied: network access to "0.0.0.0:8899", run again with the --allow-net flag
at unwrapResponse ($deno$/ops/dispatch_json.ts:43:11)
at Object.sendSync ($deno$/ops/dispatch_json.ts:72:10)
at Object.listen ($deno$/ops/net.ts:51:10)
at Object.listen ($deno$/net.ts:152:22)
at createListener (https://servestjs.org/@v1.1.0/server.ts:124:15)
at listenAndServe (https://servestjs.org/@v1.1.0/server.ts:142:20)
at Object.listen (https://servestjs.org/@v1.1.0/app.ts:81:22)
at file:///Users/hyz/work/vscode_work/deno/Servest.ts:12:5
报错提示提示我应该 使用 --allow-net
再次运行,我尝试执行命令:
$ deno run --allow-net Servest.ts
I[2020-06-12T02:46:38.136Z] servest:router listening on :8899
I[2020-06-12T02:46:46.998Z] servest:router 200 GET /
I[2020-06-12T02:46:47.279Z] servest:router 404 GET /favicon.ico
成功了。
WHY ???
原来,Deno默认情况下是安全运行的。这意味着你需要明确授予程序执行某些“特权”操作的权限,例如访问网络。
所以你需要通过 --allow-net=<domain>
参数来设置允许访问的网络。如果您不提供网络,它将允许所有,就像我上面执行的操作。
官网说明:
相关的其他参数还有:
--allow-env
Allow environment access
--allow-hrtime
Allow high resolution time measurement
--allow-net=<allow-net>
Allow network access
--allow-plugin
Allow loading plugins
--allow-read=<allow-read>
Allow file system read access
--allow-run
Allow running subprocesses
--allow-write=<allow-write>
Allow file system write access
青冥有晓月