10 quick one-liners in Deno
This article covers 10 randomly chosen one-liners in Deno. Let’s get started.
HTTP Hello world server:
Unless explicitly provided, the serve API starts a server on port 8000. The serve API (comes from standard library) is the recommended way to write HTTP servers in Deno.
Make a fetch request with timeout:
An exception with name TimeoutError will be raised after 1000ms.
Decompress gzipped body received in an HTTP request or HTTP response:
The decompressed data will be returned as a ReadableStream that can be redirected to a file or read to completion.
Append query parameters (URLSearchParams) to the fetch URL:
Test a URL against a pattern:
The output is true if match occurs, false otherwise.
Get number of CPUs present on the system:
The returned number of CPUs (logical) can be used to spawn the right number of workers.
Run a shell command and collect the output as a string:
If binary output is required, the TextDecoder part can be omitted.
Get value of an environment variable:
A string is returned that contains the value of the environment variable, NULL is returned if the environment variable doesn’t exist.
A simple static file server:
Deno provides a built-in readable stream for any open file. This readable stream can be directly given to the Response object.
Read a text file:
The read file is returned as a string.
Looking for more? The second part of the one-liner series is here:
This story is a part of the exclusive medium publication on Deno: Deno World.