Linter 说“parse”和“serveHttp”已弃用,我该如何解决这个问题?

问题描述 投票:0回答:1

我有以下打字稿代码,它是“Google Analytics 入门”课程的一部分。

我安装了 deno 1.42.1 来运行代码,但 linter 说“parse”(第 1 行)和“serveHttp”(第 18 行)已被弃用。

我没有 Typescript 或使用 Deno 的经验。只是想修复代码以使其工作。

import { parse } from "https://deno.land/[email protected]/flags/mod.ts";

const flags = parse(Deno.args, {
    string: [ "port" ],
    default: { port: 80 },
});

const server = Deno.listen({ port: flags.port });
console.log("File server running on http://localhost:" + flags.port + "/");

const __dirname = new URL(".", import.meta.url).pathname;

for await (const conn of server) {
    handleHttp(conn).catch(console.error);
}

async function handleHttp(conn: Deno.Conn) {
    const httpConn = Deno.serveHttp(conn);
    for await (const requestEvent of httpConn) {

        const url = new URL(requestEvent.request.url);
        const filepath = decodeURIComponent(url.pathname);

        let file;
        try {
            console.log(filepath);
            file = await Deno.open(__dirname + "/public" + filepath, { read: true });
        } catch {
            const notFoundResponse = new Response("404 Not Found", { status: 404 });
            await requestEvent.respondWith(notFoundResponse);
            return;
        }

        const readableStream = file.readable;
        const response = new Response(readableStream);
        await requestEvent.respondWith(response);
    }
}

以下是 Github 上的完整源代码: https://github.com/googleanalytics/ga4-tutorials?tab=readme-ov-file (代码在这个路径:tutorials-initial-setup)

typescript deno
1个回答
1
投票

https://deno.land/std/flags/mod.ts已弃用!使用 https://deno.land/std/cli/parse_args.ts 代替。

import { parseArgs } from "https://deno.land/[email protected]/cli/parse_args.ts";

const flags = parseArgs(Deno.args, {
    string: [ "port" ],
    default: { port: 80 },
});

serveHttp
是一个已弃用的 API,不鼓励使用(请使用
Deno.serve()
代替)。

© www.soinside.com 2019 - 2024. All rights reserved.