跳转到内容

Deno integration

此内容尚不支持你的语言。

⭐ Community Toolkit Deno logo

The Aspire Deno hosting integration enables you to run Deno applications alongside your Aspire projects in the Aspire app host. Deno is a modern JavaScript and TypeScript runtime.

To get started with the Aspire Deno hosting integration, install the CommunityToolkit.Aspire.Hosting.Deno NuGet package in the app host project.

Aspire CLI — 添加 CommunityToolkit.Aspire.Hosting.Deno 包
aspire add communitytoolkit-deno

Aspire CLI 是交互式的;按提示选择合适的搜索结果:

Aspire CLI — 输出示例
Select an integration to add:
> communitytoolkit-deno (CommunityToolkit.Aspire.Hosting.Deno)
> Other results listed as selectable options...

To add a Deno application to your app host, use the AddDenoApp extension method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var denoApp = builder.AddDenoApp(
name: "deno-api",
workingDirectory: "../deno-app",
scriptPath: "main.ts",
args: ["--allow-net", "--allow-env"])
.WithHttpEndpoint(port: 8000, env: "PORT");
builder.AddProject<Projects.ExampleProject>()
.WithReference(denoApp);
// After adding all resources, run the app...

The AddDenoApp method requires:

  • name: The name of the resource in the Aspire dashboard
  • workingDirectory: The path to the directory containing your Deno application
  • scriptPath: The path to the Deno script to run (relative to working directory)
  • args: Permission flags required by Deno (e.g., --allow-net, --allow-env)

To run a task defined in package.json or deno.json, use the AddDenoTask extension method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var denoApp = builder.AddDenoTask(
name: "deno-api",
workingDirectory: "../deno-app",
taskName: "start")
.WithHttpEndpoint(port: 8000, env: "PORT");
// After adding all resources, run the app...

This runs deno task start in the specified working directory.

To ensure packages are installed before running your Deno application, use the WithDenoPackageInstallation method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var denoApp = builder.AddDenoApp("deno-api", "../deno-app", "main.ts", ["--allow-net", "--allow-env"])
.WithDenoPackageInstallation()
.WithHttpEndpoint(port: 8000, env: "PORT");
// After adding all resources, run the app...

Deno applications typically use environment variables to configure the port they listen on. Use WithHttpEndpoint to configure the port and set the PORT environment variable:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var denoApp = builder.AddDenoApp("deno-api", "../deno-app", "main.ts", ["--allow-net", "--allow-env"])
.WithHttpEndpoint(port: 8000, env: "PORT");
// After adding all resources, run the app...

Your Deno application can read the PORT environment variable:

const port = Number(Deno.env.get("PORT")) || 8000;
Deno.serve({ port }, () => {
return new Response("Hello from Deno!");
});
console.log(`Server listening on port ${port}`);

The workingDirectory parameter specifies where the Deno application is located. The Aspire app host will run Deno commands in this directory.

问 & 答协作社区讨论观看