Deno restricts network access by default to keep your computer safe. You use --allow-net to let your program connect to the internet or other computers.
Network permissions (--allow-net) in Deno
deno run --allow-net your_script.ts Note: You can specify domains like --allow-net=example.com
You can allow all network access with --allow-net or restrict it to specific domains like --allow-net=example.com.
Without this flag, Deno will block any network requests to keep your system safe.
deno run --allow-net script.ts
deno.land domain.deno run --allow-net=deno.land script.ts
deno run script.ts
This simple server listens on port 8000 and responds with a greeting. You must run it with --allow-net so it can open the network port.
import { serve } from "https://deno.land/std@0.201.0/http/server.ts"; const server = serve({ port: 8000 }); console.log("Server running on http://localhost:8000/"); for await (const req of server) { req.respond({ body: "Hello from Deno!" }); }
Always grant the least permissions needed for safety.
Use domain restrictions to limit network access when possible.
If you forget --allow-net, your program will throw a permission error when trying to access the network.
--allow-net lets your Deno program use the network safely.
You can allow all or specific domains for network access.
Without it, network calls will be blocked to protect your computer.
Practice
--allow-net flag do when running a Deno program?Solution
Step 1: Understand the purpose of
This flag is used to grant network access to a Deno program, which is otherwise restricted for security.--allow-netStep 2: Compare options with the flag's function
Only "It allows the program to access the network." correctly states that--allow-netallows network access. Other options describe unrelated permissions or features.Final Answer:
It allows the program to access the network. -> Option AQuick Check:
Network permission = Allow network access [OK]
- Confusing --allow-net with file permissions
- Thinking it disables network access
- Mixing it up with debugging flags
app.ts?Solution
Step 1: Identify correct flag syntax for domain restriction
The correct syntax is--allow-net=domainplaced before the script name.Step 2: Check command order and placement
deno run --allow-net=example.com app.ts correctly places--allow-net=example.combeforeapp.ts. Other options have incorrect order or syntax.Final Answer:
deno run --allow-net=example.com app.ts -> Option CQuick Check:
Correct flag syntax = deno run --allow-net=example.com app.ts [OK]
- Placing domain after script name
- Using space instead of '=' between flag and domain
- Putting script name before flags
deno run --allow-net=deno.land app.ts
And
app.ts contains:const response = await fetch('https://deno.land/std/http/file_server.ts');
console.log(response.status);What will be the output?
Solution
Step 1: Check network permission for the domain
The command allows network access only to "deno.land", which matches the fetch URL domain.Step 2: Understand fetch behavior with permission
Since permission is granted, fetch succeeds and returns HTTP status 200 for a valid resource.Final Answer:
200 -> Option DQuick Check:
Allowed domain fetch = 200 status [OK]
- Assuming permission error despite allowed domain
- Confusing 404 with permission issues
- Expecting syntax errors from fetch
deno run --allow-net=example.com app.ts
Inside
app.ts you fetch https://google.com. What error will you get?Solution
Step 1: Analyze allowed domains
The command only allows network access to "example.com" but the script tries to fetch "google.com".Step 2: Understand Deno's permission enforcement
Deno blocks network requests to domains not allowed, causing a PermissionDenied error.Final Answer:
PermissionDenied: network access to "google.com" is not allowed -> Option AQuick Check:
Disallowed domain fetch = PermissionDenied error [OK]
- Thinking fetch will succeed without permission
- Confusing permission error with URL or reference errors
- Ignoring domain restrictions in --allow-net
api.example.com and data.service.org. Which command correctly grants network access to both domains using a single --allow-net flag?Solution
Step 1: Understand multiple domain syntax for --allow-net
Deno accepts multiple domains separated by commas in a single--allow-netflag.Step 2: Evaluate each option's correctness
deno run --allow-net=api.example.com,data.service.org app.ts uses comma-separated domains correctly. deno run --allow-net=api.example.com --allow-net=data.service.org app.ts repeats the flag (not a single flag). deno run --allow-net app.ts api.example.com data.service.org misplaces domains as arguments. deno run --allow-net=api.example.com;data.service.org app.ts uses semicolon which is incorrect.Final Answer:
deno run --allow-net=api.example.com,data.service.org app.ts -> Option BQuick Check:
Multiple domains = comma-separated in one flag [OK]
- Using multiple --allow-net flags
- Separating domains with semicolons
- Placing domains after script name
Ready to prove it?
Pass the test and this pattern moves unknown
