Consider this Deno script that tries to fetch data from a website:
const response = await fetch('https://example.com');
const text = await response.text();
console.log(text);What will happen if you run this script without the --allow-net flag?
const response = await fetch('https://example.com'); const text = await response.text(); console.log(text);
Think about what Deno does when a script tries to access the network without permission.
Deno requires explicit permission to access the network. Without --allow-net, any network request will cause a permission error and stop the script.
You want to run a Deno script that fetches data only from example.com. Which command correctly grants network permission only to that domain?
Check the correct syntax for specifying hostnames in --allow-net.
The --allow-net flag accepts hostnames (optionally with port) without protocol. So example.com is correct. Including protocol causes errors.
Look at this command and script:
deno run --allow-net=example.com script.ts
const response = await fetch('https://httpbin.org/data');
const data = await response.json();
console.log(data);Why does the script fail with a permission error?
const response = await fetch('https://httpbin.org/data'); const data = await response.json(); console.log(data);
Check if the allowed domain matches the domain in the fetch URL exactly.
The --allow-net=example.com flag only allows network access to example.com exactly. Domains like httpbin.org are not included unless explicitly allowed.
Given this script and running it with deno run --allow-net:
try {
const response = await fetch('https://httpbin.org/status/404');
console.log(response.status);
} catch (error) {
console.log('Error:', error.message);
}What will be printed?
try { const response = await fetch('https://httpbin.org/status/404'); console.log(response.status); } catch (error) { console.log('Error:', error.message); }
Think about what happens when a fetch gets a 404 status but network permission is granted.
The fetch succeeds in making the request because network permission is granted. The server responds with status 404, which is printed. No error is thrown for HTTP errors.
Which statement best explains how Deno's --allow-net flag enhances security compared to Node.js default behavior?
Think about default network access permissions in both runtimes.
Deno improves security by denying network access unless the user explicitly grants it with --allow-net. Node.js allows network access by default, which can be risky.
