Creating an HTTP Service in Deno

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

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


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

查看所有标签

猜你喜欢:

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

人工智能的未来

人工智能的未来

Jeff Hawkins、Sandra Blakeslee / 贺俊杰、李若子、杨倩 / 陕西科学技术出版社 / 2006.1 / 18.5

陕西科技出版社最新引进美国图书《人工智能的未来》(On Intelligence)一书,是由杰夫•霍金斯,一位在硅谷极其成功、受人尊敬的计算机工程师、企业家与桑德拉•布拉克斯莉,《纽约日报》的栏目作家共同撰写。本书对人类大脑皮层所具有的知觉、认识、行为和智能功能新理论提出了新的理论构想。这一理论的独到之处在于对大脑皮层的现行认识提出了新的观点,对大脑的工作原理,即霍金斯称之为“真正智能”而非计算机......一起来看看 《人工智能的未来》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具