Aller au contenu

Bun integration

Ce contenu n’est pas encore disponible dans votre langue.

⭐ 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 — Ajouter le package CommunityToolkit.Aspire.Hosting.Bun
aspire add communitytoolkit-bun

La CLI Aspire est interactive ; choisissez le résultat approprié lorsque demandé :

Aspire CLI — Exemple de sortie
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.

Questions & RéponsesCollaborerCommunautéDiscuterRegarder