📜  deno allow net - TypeScript (1)

📅  最后修改于: 2023-12-03 15:30:26.267000             🧑  作者: Mango

Deno Allow Net - TypeScript

Deno is a secure runtime for JavaScript and TypeScript. To enable network access in a Deno program, the allow-net flag is required. This flag allows a Deno program to make both HTTP and HTTPS requests to external servers.

To use the allow-net flag in a TypeScript Deno program, you must first install the Deno TypeScript module.

import { Application, Router } from "https://deno.land/x/oak/mod.ts";

The Application and Router classes are imported from the Oak module. Oak is a middleware framework for Deno that makes it easy to build web applications.

Once the Oak module is imported, create a new Application and Router instance, and use the allow-net flag to enable network access.

import { Application, Router } from "https://deno.land/x/oak/mod.ts";

const app = new Application();
const router = new Router();

app.use(router.routes());
app.use(router.allowedMethods());

await app.listen({ port: 8000, hostname: "localhost" });

In this example, an Oak Application is created and a new Router instance is created. The router will handle incoming HTTP requests.

The app.use method is used to attach the router to the app. The app.listen method starts the server on localhost:8000, allowing incoming HTTP requests.

Finally, run the program with the --allow-net flag to allow network access.

deno run --allow-net app.ts

With the allow-net flag enabled, the Deno program can make HTTP and HTTPS requests to external servers.

That's it! With the allow-net flag enabled, you can build powerful, network-enabled Deno applications with TypeScript.