On this page
Fetch and stream data
Deno brings several familiar Web APIs to the server-side environment. If you'veworked with browsers you may recognize thefetch() methodand thestreams API, which are used to make networkrequests and access streams of data over the network. Deno implements theseAPIs, allowing you to fetch and stream data from the web.
Fetching dataJump to heading
When building a web application, developers will often need to retrieveresources from somewhere else on the web. We can do so with thefetch API.We'll look at how to fetch different shapes of data from a url and how to handlean error if the request fails.
Create a new file calledfetch.js and add the following code:
// Output: JSON Dataconst jsonResponse=awaitfetch("https://api.github.com/users/denoland");const jsonData=await jsonResponse.json();console.log(jsonData,"\n");// Output: HTML Dataconst textResponse=awaitfetch("https://deno.land/");const textData=await textResponse.text();console.log(textData,"\n");// Output: Error Messagetry{awaitfetch("https://does.not.exist/");}catch(error){console.log(error);}You can run this code with thedeno run command. Because it is fetching dataacross the network, you need to grant the--allow-net permission:
deno run --allow-net fetch.jsYou should see the JSON data, HTML data as text, and an error message in theconsole.
Streaming dataJump to heading
Sometimes you may want to send or receive large files over the network. When youdon't know the size of a file in advance, streaming is a more efficient way tohandle the data. The client can read from the stream until it says it is done.
Deno provides a way to stream data using theStreams API. We'll look at how toconvert a file into a readable or writable stream and how to send and receivefiles using streams.
Create a new file calledstream.js.
We'll use thefetch API to retrieve a file. Then we'll use theDeno.open method to create and open a writable file andthepipeTo method from the Streams API tosend the byte stream to the created file.
Next, we'll use thereadable property on aPOST request to send the bytestream of the file to a server.
// Receiving a fileconst fileResponse=awaitfetch("https://deno.land/logo.svg");if(fileResponse.body){const file=await Deno.open("./logo.svg",{ write:true, create:true});await fileResponse.body.pipeTo(file.writable);}// Sending a fileconst file=await Deno.open("./logo.svg",{ read:true});awaitfetch("https://example.com/",{ method:"POST", body: file.readable,});You can run this code with thedeno run command. Because it is fetching dataacross the network and writing to a file, you need to grant the--allow-net,--allow-write and--allow-read permissions:
deno run --allow-read --allow-write --allow-net stream.jsYou should see the filelogo.svg created and populated in the currentdirectory and, if you ownedexample.com you would see the file being sent to theserver.
🦕 Now you know how to fetch and stream data across a network and how to streamthat data to and from files! Whether you're serving static files, processinguploads, generating dynamic content or streaming large datasets, Deno’s filehandling and streaming capabilities are great tools to have in your developertoolbox!