ServestDeno 的渐进式 HTTP 服务器

联合创作 · 2023-10-01 14:35

Servest 是 Deno 的 HTTP 模块套件,它由 HTTP 协议相关的三个主要 API 组成:

  • Router API:通用 HTTP 路由服务器。
  • Server API:用于处理 HTTP/1.1 请求的低级 HTTP API。
  • Agent API:用于管理与主机的 HTTP/1.1 Keep-Alive 连接的低级 API。
// @deno-types="https://servestjs.org/@v1.0.0-rc2/types/react/index.d.ts"
import React from "https://dev.jspm.io/react/index.js";
// @deno-types="https://servestjs.org/@v1.0.0-rc2/types/react-dom/server/index.d.ts"
import ReactDOMServer from "https://dev.jspm.io/react-dom/server.js";
import { createApp } from "https://servestjs.org/@v1.0.0-rc2/mod.ts";

const app = createApp();
app.handle("/", async (req) => {
  await req.respond({
    status: 200,
    headers: new Headers({
      "content-type": "text/html; charset=UTF-8",
    }),
    body: ReactDOMServer.renderToString(
      <html>
        <head>
          <meta charSet="utf-8" />
          <title>servest</title>
        </head>
        <body>Hello Servest!</body>
      </html>,
    ),
  });
});
app.listen({ port: 8899 });
浏览 14
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报