10 quick one-liners in Deno — Part 2
This article is a continuation of the one-liner series. The first article is here:
Let’s get started with 10 more quick one-liners.
1
HTTPS hello world server:
Unless HTTP/2 is required, the serveTls API (comes from standard library) is the recommended way to write secure HTTP servers in Deno.
2
Redirect an HTTP request:
Redirecting an HTTP request requires preparing a Response object through the web standard Response.redirect API.
3
Upload a local file with fetch API:
Opening a file comes with Readable and Writable streams that can be attached to Request or Response objects.
4
Save an uploaded file on local disk:
The received stream (req.body) can be piped to a compatible destination that implements WritableStream.
5
Get size of a file:
The Deno.stat API returns a number of file properties, size is one of them.
6
Write a string to a new file or overwrite an existing file:
A file is created if it doesn’t exist, or truncated if it exists.
7
Prompt a user on CLI with a default value:
The default value is shown with the prompt and gets returned if the user presses enter without providing a value.
8
Send JSON with fetch API:
As JSON is not a standard supported data type, content-type header needs to be set explicitly.
9
Copy contents of a file using pipe:
The same web standard API pipeTo can be used to copy contents of a file into another file.
10
Zip a received file and then save to disk:
The request stream is first piped through web standard CompressionStream and the output zipped stream is then piped to the destination file’s WritableStream.
The first 10 one-liners can be checked here: