콘텐츠로 이동

Bun integration

이 콘텐츠는 아직 번역되지 않았습니다.

⭐ Community Toolkit Bun logo

The Aspire Bun hosting integration enables you to run Bun applications alongside your Aspire projects in the Aspire app host. Bun is a fast JavaScript runtime and toolkit.

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

Aspire CLI — CommunityToolkit.Aspire.Hosting.Bun 패키지 추가
aspire add communitytoolkit-bun

Aspire CLI는 대화형입니다. 프롬프트 시 알맞은 검색 결과 선택:

Aspire CLI — 출력 예시
Select an integration to add:
> communitytoolkit-bun (CommunityToolkit.Aspire.Hosting.Bun)
> Other results listed as selectable options...

To add a Bun application to your app host, use the AddBunApp extension method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var bunApp = builder.AddBunApp(
name: "bun-api",
workingDirectory: "../bun-app")
.WithHttpEndpoint(port: 3000, env: "PORT");
builder.AddProject<Projects.ExampleProject>()
.WithReference(bunApp);
// After adding all resources, run the app...

The AddBunApp method requires:

  • name: The name of the resource in the Aspire dashboard
  • workingDirectory: The path to the directory containing your Bun application

By default, the integration looks for an index.ts file as the entrypoint.

To ensure packages are installed before running your Bun application, use the WithBunPackageInstaller method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var bunApp = builder.AddBunApp("bun-api", "../bun-app")
.WithBunPackageInstaller()
.WithHttpEndpoint(port: 3000, env: "PORT");
// After adding all resources, run the app...

This runs bun install before starting your application.

To specify a custom entrypoint file, pass it as an additional parameter:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var bunApp = builder.AddBunApp("bun-api", "../bun-app", "server.ts")
.WithHttpEndpoint(port: 3000, env: "PORT");
// After adding all resources, run the app...

Bun 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 bunApp = builder.AddBunApp("bun-api", "../bun-app")
.WithHttpEndpoint(port: 3000, env: "PORT");
// After adding all resources, run the app...

Your Bun application can read the PORT environment variable:

const server = Bun.serve({
port: process.env.PORT || 3000,
fetch(request) {
return new Response('Hello from Bun!');
},
});
console.log(`Server listening on port ${server.port}`);

The workingDirectory parameter specifies where the Bun application is located. The Aspire app host will run bun run in this directory to start your Bun application.