跳转到内容
Docs Try Aspire
Docs Try

Deno integration

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

Deno logo ⭐ Community Toolkit

The Aspire Deno hosting integration enables you to run Deno applications — a modern, secure JavaScript and TypeScript runtime — alongside your other resources in the AppHost.

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...

Use the AddDenoApp extension method to add a Deno application to your AppHost:

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>("api")
.WithReference(denoApp);
builder.Build().Run();

The AddDenoApp method accepts the following parameters:

ParameterDescription
nameThe name of the resource in the Aspire dashboard.
workingDirectoryPath to the directory containing your Deno application.
scriptPathPath to the Deno script to run, relative to the working directory.
argsDeno permission flags and other arguments passed at startup.

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

AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var denoApp = builder.AddDenoTask(
name: "deno-api",
workingDirectory: "../deno-app",
taskName: "start")
.WithHttpEndpoint(port: 8000, env: "PORT");
builder.Build().Run();

This runs deno task start in the specified working directory.

To ensure packages are installed before running your Deno application, call WithDenoPackageInstallation:

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"])
.WithDenoPackageInstallation()
.WithHttpEndpoint(port: 8000, env: "PORT");
builder.Build().Run();

Deno applications read the port to listen on from an environment variable. Use WithHttpEndpoint to expose a port and inject it into the process:

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.Build().Run();

Inside your Deno application, read the PORT environment variable using Deno.env.get:

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