【问题标题】:ASP.NET Core project with NextJS带有 NextJS 的 ASP.NET Core 项目
【发布时间】:2023-03-24 05:50:02
【问题描述】:

我使用 Visual Studio 2019 创建了一个带有 React.js 模板的 ASP.NET Core 3.1 项目

当我将项目作为 React.js 应用程序运行时,一切正常。但是当我将应用程序更改为 NextJS 应用程序时,当我运行它时,我给了我错误:

AggregateException: One or more errors occurred.
(One or more errors occurred. (The NPM script 'start' exited without indicating that the create-react-app server was listening for requests.
The error output was: Error: Could not find a valid build in the 'C:\Users\myname\source\repos\Panel\Panel\ClientApp\.next' directory! Try building your app with 'next build' before starting the server.

我可以使用 npm run dev 运行应用程序,但我想在运行 ASP.NET Core 应用程序的同一端口上使用 Visual Studio 运行应用程序,例如 React.js 应用程序。

如何添加 npm run dev 之类的命令并执行它们以在同一端口上运行应用程序?

【问题讨论】:

    标签: node.js reactjs asp.net-core next.js csproj


    【解决方案1】:

    您需要在 Startup.cs 中使用反向代理来指向用于启动 next.js 应用程序的脚本。例如:

    app.UseSpa(spa =>
    {
        spa.Options.SourcePath = "ClientApp/nextjs-client";
    
        if (env.IsDevelopment())
        {
            spa.Options.StartupTimeout = TimeSpan.FromSeconds(120);
            spa.UseProxyToSpaDevelopmentServer("http://localhost:3000");
            spa.UseReactDevelopmentServer(npmScript: "dev");
        }
    });
    

    【讨论】:

    • 这适用于开发模式,但是否有类似的解决方案可以在单个 azure 应用服务容器中使用 nextjs 并为“else”案例使用“next start”脚本?
    • @NateRadebaugh 我通常使用带有单个 dockerfile 的 Docker Compose 来准备 nextjs。我目前的项目仍在开发中,但我很快就能在 azure 上对其进行测试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 2018-06-18
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多