10 quick one-liners in Deno

Mayank C
Tech Tonic

--

This article covers 10 randomly chosen one-liners in Deno. Let’s get started.

1

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.

2

Make a fetch request with timeout:

An exception with name TimeoutError will be raised after 1000ms.

3

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.

4

Append query parameters (URLSearchParams) to the fetch URL:

5

Test a URL against a pattern:

The output is true if match occurs, false otherwise.

6

Get number of CPUs present on the system:

The returned number of CPUs (logical) can be used to spawn the right number of workers.

7

Run a shell command and collect the output as a string:

If binary output is required, the TextDecoder part can be omitted.

8

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.

9

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.

10

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:

--

--