Creating an HTTP Service in Deno

栏目: IT技术 · 发布时间: 5年前

内容简介: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:

http://localhost:8000/

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.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

自品牌

自品牌

陈为、孙郁婷 / 机械工业出版社 / 2015-9-7 / 39

移动互联网来势汹涌,让品牌重新回到人的时代。微信旗帜鲜明地宣示,“再小的个体也有自己的品牌”。《自品牌:个人如何玩转移动互联网时代》作者历经一年,深度访谈10位嘉宾,挖掘其品牌与商业成功密码。吴晓波、雕爷、罗永浩、鬼脚七、马佳佳……这些商业新浪潮中的探路者与领军者,要么是传统领域的老将,要么是新领域里的先锋,但都能以新媒体为载体,构建个人品牌,打造商业生态,抓住互联网的时代红利,顺风而起,顺势而为......一起来看看 《自品牌》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具