美文网首页
7.7 Deno HTTP Web服务器

7.7 Deno HTTP Web服务器

作者: 9e8aeff1c70c | 来源:发表于2021-07-05 09:44 被阅读0次

概念

  • 使用Deno的集成HTTP服务器运行您自己的Web服务器。

概览

只需几行代码,您就可以运行自己的HTTP Web服务器,并控制响应状态、请求头等。

ℹ️ 原生的HTTP服务器目前不稳定,这意味着API还没有定稿,在未来版本的Deno中可能会发生破坏性的变化。要让这里讨论的API可用,您必须使用--unstable标志运行deno。

web server例子

在此示例中,客户端的用户代理返回给客户端:

webserver.ts:

// Start listening on port 8080 of localhost.
const server = Deno.listen({ port: 8080 });
console.log(`HTTP webserver running.  Access it at:  http://localhost:8080/`);

// Connections to the server will be yielded up as an async iterable.
for await (const conn of server) {
  // In order to not be blocking, we need to handle each connection individually
  // in its own async function.
  (async () => {
    // This "upgrades" a network connection into an HTTP connection.
    const httpConn = Deno.serveHttp(conn);
    // Each request sent over the HTTP connection will be yielded as an async
    // iterator from the HTTP connection.
    for await (const requestEvent of httpConn) {
      // The native HTTP server uses the web standard `Request` and `Response`
      // objects.
      const body = `Your user-agent is:\n\n${requestEvent.request.headers.get(
        "user-agent",
      ) ?? "Unknown"}`;
      // The requestEvent's `.respondWith()` method is how we send the response
      // back to the client.
      requestEvent.respondWith(
        new Response(body, {
          status: 200,
        }),
      );
    }
  })();
}

然后运行下面命令:

deno run --allow-net --unstable webserver.ts

然后在浏览器中导航到http://localhost:8080/‘。

使用 std/http

如果您不想使用不稳定的API,您仍然可以使用标准库的HTTP服务器:

webserver.ts:

import { serve } from "https://deno.land/std@0.95.0/http/server.ts";

const server = serve({ port: 8080 });
console.log(`HTTP webserver running.  Access it at:  http://localhost:8080/`);

for await (const request of server) {
  let bodyContent = "Your user-agent is:\n\n";
  bodyContent += request.headers.get("user-agent") || "Unknown";

  request.respond({ status: 200, body: bodyContent });
}

然后运行下面的命令:

deno run --allow-net webserver.ts

相关文章

  • 7.7 Deno HTTP Web服务器

    概念 使用Deno的集成HTTP服务器运行您自己的Web服务器。 [https://deno.land/manua...

  • 3.5 Deno HTTP服务器API

    从Deno 1.9和更高版本开始,引入了native HTTP服务器API,这些API使用户可以在Deno中创建功...

  • CentOS7.7下docker部署OpenWAF

    CentOS7.7下docker部署OpenWAF 服务器操作系统:CentOS7.7 64位 官方地址:http...

  • Web Server、HTTP Server、Applicati

    由于Web服务器主要支持的协议就是HTTP,一般 Web Server == HTTP Server。Web服务器...

  • HTTP协议

    * HTTP协议简介 * HTTP版本 * HTTP报文 * Web服务器 * HTTPS * Web安全防范 *...

  • nodejs创建web服务器和Tcp服务器

    使用http模块创建Web服务器 Web服务器的功能: 接受HTTP请求(GET、POST、DELETE、PUT、...

  • 4.3 代理

    Deno支持模块下载代理和Web标准FETCH API。 代理配置从以下环境变量读取:HTTP_PROXY、HTT...

  • [drash] Drash 介绍

    介绍 Drash 是 Deno 的 HTTP 服务器的 REST 微框架,零依赖。 Drash 的设计目的是帮助您...

  • Web服务器

    含义 Web服务器可以解析(handles)HTTP协议。当Web服务器接收到一个HTTP请求(request),...

  • <HTTP权威指南>读书笔记 ---- Web服

    Web服务器 Web服务器的实现 Web服务器会对HTTP请求进行处理并提供响应。术语"Web服务器"可以用来表示...

网友评论

      本文标题:7.7 Deno HTTP Web服务器

      本文链接:https://www.haomeiwen.com/subject/jmiurltx.html