The JS runtimes
Published in

The JS runtimes

HTTP echo server in Deno

Introduction

Problem

$ curl http://localhost:8000 -v
> GET / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.77.0
> Accept: */*
>
< HTTP/1.1 200 OK
< accept: */*
< host: localhost:8000
< user-agent: curl/7.77.0
< content-length: 0
< date: Sat, 08 Jan 2022 19:11:23 GMT
$ curl http://localhost:8000 -v -d 'a=b' -d 'c=2'
> POST / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.77.0
> Accept: */*
> Content-Length: 7
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 200 OK
< accept: */*
< content-length: 7
< content-type: application/x-www-form-urlencoded
< host: localhost:8000
< user-agent: curl/7.77.0
< date: Sat, 08 Jan 2022 19:12:12 GMT
<
a=b&c=2
$ curl http://localhost:8000 -v -d '{"a": "b", "c": "d"}' -H 'content-type: application/json' -H 'some-other-hdr: some-other-hdr-value'
> POST / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.77.0
> Accept: */*
> content-type: application/json
> some-other-hdr: some-other-hdr-value
> Content-Length: 20
>
< HTTP/1.1 200 OK
< accept: */*
< content-length: 20
< content-type: application/json
< host: localhost:8000
< some-other-hdr: some-other-hdr-value
< user-agent: curl/7.77.0
< date: Sat, 08 Jan 2022 19:16:33 GMT
<
{"a": "b", "c": "d"}
$ curl http://localhost:8000 -v -F 'a=b' -F 'file=@/var/tmp/testdata/sample.txt'
> POST / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.77.0
> Accept: */*
> Content-Length: 301
> Content-Type: multipart/form-data; boundary=------------------------2639aa4a6f4e6555
>
< HTTP/1.1 200 OK
< accept: */*
< content-length: 301
< content-type: multipart/form-data; boundary=------------------------2639aa4a6f4e6555
< host: localhost:8000
< user-agent: curl/7.77.0
< date: Sat, 08 Jan 2022 19:18:00 GMT
<
--------------------------2639aa4a6f4e6555
Content-Disposition: form-data; name="a"
b
--------------------------2639aa4a6f4e6555
Content-Disposition: form-data; name="file"; filename="sample.txt"
Content-Type: text/plain
Learning Deno Is Fun!--------------------------2639aa4a6f4e6555--

Echo server

Imports

import { serve } from "https://deno.land/std/http/mod.ts";

The echo server

//app.tsimport { serve } from "https://deno.land/std/http/mod.ts";async function reqHandler(req: Request) {
return new Response(req.body, {
headers: req.headers,
});
}
serve(reqHandler, { port: 8000 });

Tests

$ deno run --allow-net=:8000 app.ts$ curl http://localhost:8000 -v -d 'Hello world' -H 'content-type: text/plain'
> POST / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.77.0
> Accept: */*
> content-type: text/plain
> Content-Length: 11
>
< HTTP/1.1 200 OK
< accept: */*
< content-length: 11
< content-type: text/plain
< host: localhost:8000
< user-agent: curl/7.77.0
< date: Sat, 08 Jan 2022 19:23:10 GMT
<
Hello world
$ ls -l /var/tmp/testdata/sample.pdf 
-rw-r--r-- 1 mayankc staff 69273 Apr 3 2021 /var/tmp/testdata/sample.pdf
$ curl http://localhost:8000 -v --data-binary "@/var/tmp/testdata/sample.pdf" -H 'content-type: application/pdf' -o /dev/null -sL
> POST / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.77.0
> Accept: */*
> content-type: application/pdf
> Content-Length: 69273
>
< HTTP/1.1 200 OK
< accept: */*
< content-length: 69273
< content-type: application/pdf
< host: localhost:8000
< user-agent: curl/7.77.0
< date: Sat, 08 Jan 2022 19:38:43 GMT
<
/* OUTPUT REDIRECTED TO /DEV/NULL */

Echo service

https://echo-server.deno.dev
$ curl https://echo-server.deno.dev -v --data-binary '{"name":"John", "age":30, "car":null}' -H 'content-type: application/json' -sL
> POST / HTTP/2
> Host: echo-server.deno.dev
> user-agent: curl/7.77.0
> accept: */*
> content-type: application/json
> content-length: 37
>
< HTTP/2 200
< accept: */*
< content-length: 37
< content-type: application/json
< host: echo-server.deno.dev
< user-agent: curl/7.77.0
< x-forwarded-for: 2601:646:8200:6180:6136:1672:1409:6172
< date: Sat, 08 Jan 2022 19:43:11 GMT
< server: deploy/us-west2-a
<
{"name":"John", "age":30, "car":null}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store