Book Deno Web Development ↩
|
Directory portela-examples\ contains TypeScript examples presented in Portela's book Deno Web Development (Packt, 2021).
|
Example http-server.ts
makes use of the function serve
from the remote http
library. The import clauses looks as follows:
// file: http-server.ts import { serve } from "https://deno.land/std@0.208.0/http/server.ts" for await (const req of serve(":8080")) { req.respond({ body: "Hello deno" }) }
We can run http-server
either from the Windows prompt or from a Unix shell:
-
We run the batch file
http-server.bat
from the Windows prompt. Note that the server process is started only once (it must be stopped manually) when we run the batch file several times:> http-server.bat [INFO] Start process listening on port 8080 [INFO] Respond to the cURL request on port 8080 Hello deno > http-server.bat [INFO] Respond to the cURL request on port 8080 Hello deno
-
We run the shell script
http-server.sh
from a Unix shell (Cygwin, MinGW or Unix).> ./http-server.sh [INFO] Start process listening on port 8080 [INFO] Respond to the cURL request on port 8080 Hello deno > ./http-server.sh [INFO] Respond to the cURL request on port 8080 Hello deno
http-server-deps
Example ▴
In this first variant of the above example we split the code into two files:
-
we add the file
deps.ts
which contains the original import clause(s)// file: deps.ts import { serve } from "https://deno.land/std@0.190.0/http/server.ts"
-
we modify the file
https-server.ts
which now refers todeps.ts
in the import clause (thus hiding the details of the remotestd/http
library):// file: http-server.ts import { serve } from "./deps.ts" for await (const req of serve(":8080")) { req.respond({ body: "Hello deno" }) }
Example http-server-import-maps
is the second variant of the original example presented earlier. Besides the two files http-server.ts
and http-server.bat
we also :
-
add the file
import_map.json
{ "imports": { "http/": "https://deno.land/std@0.197.0/http/" } }
-
and update the file
deps.ts
with a user-defined name for thehttp
library.// file: deps.ts export { serve } from "http/server.ts"
fetch-deno-logo
Example ▴
Example fetch-deno-logo
download a SVG image (e.g. the Deno logo) from the Internet and prints the HTML code with the embedded image.
> fetch-deno-logo.bat <html> <img src="data:image/svg+xml;base64,PHN2ZwogIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIKICB3aWR0aD0iNTEyIgogIGhlaWdodD0iNTEyIgogIHZpZXdCb3g9IjAgMCA1MTIgNTEyI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