Skip to content
Docs Try Aspire
Docs Try

Integrating a Node.js app within an Aspire application

Aspire sample
C# AppHost

Clone, run, and explore this sample

This sample demonstrates integrating a Node.js app and an ASP.NET Core HTTP API using Aspire.

C#JavaScriptNode.jsRedis
AppHost

The entry point that composes every resource and dependency in this sample's distributed application.

View on GitHub
AppHost.cs
#pragma warning disable ASPIREBROWSERLOGS001
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache")
.WithRedisInsight();
var weatherapi = builder.AddProject<Projects.AspireWithNode_AspNetCoreApi>("weatherapi")
.WithHttpHealthCheck("/health");
builder.AddNodeApp("frontend", "../NodeFrontend", "./app.js")
.WithNpm()
.WithRunScript("dev")
.WithHttpEndpoint(port: 5223, env: "PORT")
.WithExternalHttpEndpoints()
.WithHttpHealthCheck("/health")
.WithBrowserLogs()
.WithReference(weatherapi).WaitFor(weatherapi)
.WithReference(cache).WaitFor(cache);
builder.Build().Run();

The sample consists of two apps:

  • NodeFrontend: This is a simple Express-based Node.js app that renders a table of weather forecasts retrieved from a backend API and utilizes a Redis cache.
  • AspireWithNode.AspNetCoreApi: This is an ASP.NET Core HTTP API that returns randomly generated weather forecast data.

If using the Aspire CLI, run aspire run from this directory.

If using VS Code, open this directory as a workspace and launch the AspireWithNode.AppHost project using either the Aspire or C# debuggers.

If using Visual Studio, open the solution file AspireWithNode.slnx and launch/debug the AspireWithNode.AppHost project.

If using the .NET CLI, run dotnet run from the AspireWithNode.AppHost directory.