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.


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

查看所有标签

猜你喜欢:

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

Java Web编程从入门到实践

Java Web编程从入门到实践

徐林林 / 清华大学出版社 / 2010-3 / 59.80元

《Java Web编程从入门到实践》内容简介:Java Web开发是目前最流行的网络开发技术之一。《Java Web编程从入门到实践》由浅入深,结合大量的实例系统地讲解了关于Java Web开发方面的知识。全书内容包括Java Web开发的基础知识、Java Web开发环境的搭建、JSP技术详解、Servlet技术详解、JSP+Servlet+JavaBean开发模式、JDBC接口的使用方法、Hi......一起来看看 《Java Web编程从入门到实践》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

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

正则表达式在线测试