内容简介:tl;drBefore we write our service, you need to
tl;dr
import { serve } from "https://deno.land/<a href="/cdn-cgi/l/email-protection" data-cfemail="106364745066203e23263e20">[email protected]</a>/http/server.ts"; const s = serve({ port: 8000 }); console.log("http://localhost:8000/"); for await (const req of s) { req.respond({ body: "Hello World\n" }); }
Deno is a new runtime for JavaScript and TypeScript created by Ryan Dahl , whose previous work includes creating Node.js .
Before we write our service, you need to install or otherwise have a way to execute code using the Deno runtime. I’m sure there are Docker images for the latter, I opted to install locally on a Windows 10 machine using the Powershell install script .
To get started, create a main.ts
file and add the following code:
import { serve } from "https://deno.land/<a href="/cdn-cgi/l/email-protection" data-cfemail="196a6d7d596f29372a2f3729">[email protected]</a>/http/server.ts"; const s = serve({ port: 8000 }); console.log("http://localhost:8000/"); for await (const req of s) { req.respond({ body: "Hello World\n" }); }
Before we dive into the code, let’s run the script with deno main.ts
.
You might see an error when trying to run the script:
Uncaught PermissionDenied: network access to “127.0.0.1:8000”, run again with the –allow-net flag, info on issue here https://deno.land/std/manual.md
By default, Deno does not allow your code to access the network. From the Deno docs :
For security reasons, Deno does not allow programs to access the network without explicit permission.
We use the --allow-net
flag to allow network access:
deno --allow-net main.ts
If all is well, Deno should write to the console that our new HTTP service is running:
You can send an HTTP GET request
to our service to verify that it’s working. To do that, I’ll use cURL
: curl localhost:8000
. If all is well, you’ll receive an HTTP response with “Hello World” in the response body.
Cool!
So, how does it work? Let’s take it line by line.
Line 1
import { serve } from "https://deno.land/<a href="/cdn-cgi/l/email-protection" data-cfemail="394a4d5d794f09170a0f1709">[email protected]</a>/http/server.ts";
Deno can import libraries directly from URLs and that’s what we’re doing here. In our case, we’re importing a function called serve
from the file located at
https://deno.land/ [email protected]
/http/server.ts
.
Line 2
const s = serve({ port: 8000 });
We’re now invoking the serve
function that we imported on line 1. The serve
function creates a new HTTP server and bids it to the port specified – in our case that’s 8000. You can inspect the serve
function here
.
Line 3
console.log("http://localhost:8000/");
This write writes out http://localhost:8000
to the console.
Line 4
for await (const req of s) {
Line 2 created a variable s
and stored the result of the expression serve({ port: 8000 });
in it. Variable s
is now an instance of a class called Server
that implements AsyncIterable<ServerRequest>
. You can read more about the Server
class particulars in the Deno docs
.
For our purposes, we just need to know that Line 4 opens a
for..of
iterator
that allows us to respond to each HTTP request.
Line 5
req.respond({ body: "Hello World\n" });
This line is important – it runs each time our server receives an HTTP request! In fact, the whole body of the for..of
iterator is executed each time our HTTP service receives an HTTP request. To give you some perspective, DuckDuckGo
averaged 61 million HTTP requests a day
in April. That means that whatever code you have on Line 5 would run 61 million times a day if your code were serving that traffic.
For fun, try changing the "Hello World\n"
text to something else, maybe "Hello from the recent past!\n"
. If you restart our service by killing and re-running the script you can use our cURL command to see your new text: curl localhost:8000
.
Line 6
}
This line closes the for..of
iterator that we opened with Line 4.
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
解构产品经理:互联网产品策划入门宝典
电子工业出版社 / 2018-1 / 65
《解构产品经理:互联网产品策划入门宝典》以作者丰富的职业背景及著名互联网公司的工作经验为基础,从基本概念、方法论和工具的解构入手,配合大量正面或负面的案例,完整、详细、生动地讲述了一个互联网产品经理入门所需的基础知识。同时,在此基础上,将这些知识拓展出互联网产品策划的领域,融入日常工作生活中,以求职、沟通等场景为例,引导读者将知识升华为思维方式。 《解构产品经理:互联网产品策划入门宝典》适合......一起来看看 《解构产品经理:互联网产品策划入门宝典》 这本书的介绍吧!