Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62
Subscribe to my email list now at http://jauyeung.net/subscribe/
Deno is a new server-side runtime environment for running JavaScript and TypeScript apps.
In this article, we’ll take a look at how to get started with developing apps for Deno.
Simple HTTP Web Server
We can create a simple HTTP server with the server
module provided by Deno.
For example, we can write:
import { serve } from "https://deno.land/std@0.75.0/http/server.ts";
const server = serve({ hostname: "0.0.0.0", port: 8080 });
console.log(`HTTP server running on port 8080`);
for await (const request of server) {
const bodyContent = `Your user-agent is: ${request.headers.get("user-agent") || "Unknown"}`;
request.respond({ status: 200, body: bodyContent });
}
We import the serve
function to create our HTTP server.
The object has the hostname
and port
to listen to requests from.
Then we create the response body in the for-await-of loop.
We get the header by calling request.headers.get
.
Then we call request.response
with the HTTP status code and body in the object that we use as the argument of respond
.
Then we can run the script with:
deno run --allow-net index.ts
Then when we go to http://localhost:8080, we should see something like:
Your user-agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36
File Server
We can also create a file server easily with Deno.
To do this, we just run:
deno run --allow-net --allow-read https://deno.land/std@0.75.0/http/file_server.ts
Then when we go to http://localhost:4507/, we see the files in the current folder listed.
TCP Echo Server
We can create a TCP echo server easily with the Deno.listen
and Deno.copy
methods.
For example, we can write:
const listener = Deno.listen({ port: 8080 });
console.log("listening on 0.0.0.0:8080");
for await (const conn of listener) {
Deno.copy(conn, conn);
}
to listen to port 8080.
Then we loop through the listener
array to watch for requests.
Then we call Deno.copy
to redirect the connection data inbound.
Creating a Subprocess
We can create a subprocess by using the Deno.run
method.
For example, we can run:
const p = Deno.run({
cmd: ["echo", "hello"],
});
await p.status();
to run echo hello
.
Then we can wait for its completion with the p.status
method.
We need the --allow-run
flag to let us run subprocesses.
We can get results from subprocesses and run with them by using various methods.
For instance, we can write:
const fileNames = Deno.args;
const p = Deno.run({
cmd: [
"deno",
"run",
"--allow-read",
"https://deno.land/std@0.75.0/examples/cat.ts",
...fileNames,
],
stdout: "piped",
stderr: "piped",
});
const { code } = await p.status();
if (code === 0) {
const rawOutput = await p.output();
await Deno.stdout.write(rawOutput);
} else {
const rawError = await p.stderrOutput();
const errorString = new TextDecoder().decode(rawError);
console.log(errorString);
}
Deno.exit(code);
We call the status
method to get the status code.
Then we write the output if the code is 0.
Otherwise, we output the error to the stderr
.
And we log the string.
And finally, we call Deno.exit
with the exit code of our command.
Conclusion
We can create a simple HTTP server, file server, and run subprocesses easily with Deno.