Automatically start a Node development server alongside with an ASP.NET Core application.
The .NET SDK must be installed.
Node.js also needs to be installed in order to start a Node development server.
If you don't have one already, create a new ASP.NET Core application.
dotnet new web --output MyAspNetCoreApp
Add the Bruce965.NodeDevServer NuGet package to your ASP.NET Core application.
dotnet add MyAspNetCoreApp package Bruce965.NodeDevServer
If you don't have one already, create a new Node.js application. You may use Vite or any other framework at your preference.
npm create -y vite -- my-frontend --template vanilla
Configure the Node development server in your Program.cs file.
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// Configure the local Node development server.
builder.Services.AddNodeDevServer(options =>
{
// You may need to tweak these options if you don't use Vite.
options.HostUri = "http://localhost:5173";
options.Path = "../my-frontend";
options.LaunchScript = "dev";
options.PackageManagers = ["yarn", "npm"];
});
WebApplication app = builder.Build();
// Some Node.js frameworks require this in order to support hot-reload.
app.UseWebSockets();
app.UseRouting();
app.UseEndpoints(_ => {});
if (app.Environment.IsDevelopment())
{
// In development, forward all requests to Node.js.
// The first request will automatically launch it.
app.UseNodeDevServer();
}
else
{
// In production, use the pre-built files.
app.UseStaticFiles();
}
app.Run();
This project is licensed under the MIT license.
Some components may be available elsewhere under different license terms, please refer to the individual source files.