0

在Deno运行时使用以下简单的TypeScript代码:

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

async function main() {
    const body = new TextEncoder().encode("Hello World\n");
    let port = 8000
    const s = serve({ port: port });
    console.log(`Server had been started at:
    http://localhost:${port}/`);
    for await (const req of s) {
      req.respond({ body });
    }
};

main()

通过运行执行:

D:\repos\deno>deno --allow-net main.ts
Compile file:///D:/repos/deno/main.ts
Server had been started at:
    http://localhost:8000/

Hello World在浏览器中显示 a 。

有没有办法显示 HTML 元素而不是文本,例如显示<h1><button>

4

2 回答 2

1

只需在字符串中添加您的 html

例子

const body = new TextEncoder().encode("<h1>Hello World</h1>\n");
于 2020-01-01T20:46:50.133 回答
0

您可以像这样在字符串中添加 html

const body = new TextEncoder().encode("<h1>Hello World</h1>");

如果你想要多个 html 元素,你可以像这样使用模板文字/模板字符串

const body = new TextEncoder().encode(`
    <div style="display: flex; justify-content: center; align-items: center;">
        <h1>Ryan Dahl From Future</h1>
        <p>Do you need another js runtime ?</p>
    </div>
`);
于 2020-05-14T18:19:43.187 回答